首页 新闻 会员 周边

为什么我选第一个下拉列表管用,第二个下拉列表不管用啊

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-18 17:32

 $(document).ready(function () {
        $('#mySelect').change(function () {
            $('option').each(function () {
                var item = $('#mySelect').val();
                var price = $('#price').text();
                $('#paid-price').html(item * price);                   
            });
                     
        });
    });

 

 <tr>
      
       <td>@item.Name</td>
       <td>
        <select id="mySelect">
        <option>0.5</option>
        <option>1</option>
        <option>1.5</option>
        <option>2</option>
        </select>
       </td>
        <td id="price">
        @item.Price
        </td>
        <td id="paid-price">
        
        </td>
       </tr>

sweetcode的主页 sweetcode | 初学一级 | 园豆:80
提问于:2013-03-18 14:28
< >
分享
最佳答案
0

不明白$('option').each(function () {的意义...

$('#mySelect').val();是获得当前下拉框的值,一个change事件中是一定的啊..

收获园豆:10
飞来飞去 | 老鸟四级 |园豆:2057 | 2013-03-18 14:34

哦,你有很多行,每行都有个id为mySelect的下拉框?id不能一样啊,你给下拉框设置不同的id吧,比如mySelect+行号

飞来飞去 | 园豆:2057 (老鸟四级) | 2013-03-18 14:36

去掉这个也没法实现 $('#mySelect').val();只能绑定到第一个下拉列表

sweetcode | 园豆:80 (初学一级) | 2013-03-18 14:36

@sweetcode: 

你可以这么写 

$(document).ready(function () {
            $("select").each(function (index) {
                $(this).bind("change", sl_change);
            });
        });
        function sl_change(event) {
            $(event.target).parent().next().next().html($(event.target).parent().next().text() * $(event.target).val());
        }
飞来飞去 | 园豆:2057 (老鸟四级) | 2013-03-18 14:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册