duanmin0941 2014-01-21 03:16
浏览 18
已采纳

根据下拉菜单ID显示数据

HTML and PHP

<?php for($i=0;$i<2;$i++) { ?>
<select id="name_<?php echo $i;?>">
    <option value="John">John</option>
    <option value="Alice">Alice</option>
</select>

<input type="text" id="name-id_<?php echo $i;?>">
<input type="text" id="location_<?php echo $i;?>">

<?php } ?>

JS

for (var i = 0; i < 2; i++) 
{
    $('select#name_'+i).on('change',function()  
    {
        name = $('select#name_'+i).val();
        if($.trim(name) !='')
        {
            $.post('ajax/name.php',{name:name},function(data)
            {
                $('input#name-id_'+i).val(data);
            });

            $.post('ajax/location.php',{name:name},function(data)
            {
                $('input#location_'+i).val(data);
            });
        }
    });
}

ajax/name.php and ajax/location.php are the files that retrieve data from DB. The output that I want is the data will appear in the name and location column according to the drop down menu data that I chose.

  • 写回答

1条回答 默认 最新

  • doujie1917 2014-01-21 03:18
    关注

    This is a classic problem with use of a closure variable in a loop, the closure variable i is shared between all the change handlers, so at the end of the loop the value of i in all the change handlers will be 2 which will not match any elements

    One solution in this case is to create a local closure for each loop iteration using IIFE

    for (var i = 0; i < 2; i++) {
        (function (idx) {
            $('select#name_' + idx).on('change', function () {
                name = $(this).val();
                if ($.trim(name) != '') {
                    $.post('ajax/name.php', {
                        name: name
                    }, function (data) {
                        $('input#name-id_' + idx).val(data);
                    });
    
                    $.post('ajax/location.php', {
                        name: name
                    }, function (data) {
                        $('input#location_' + idx).val(data);
                    });
    
                }
            });
    
        })(i)
    }
    

    Another possible solution in this case may be is to use event data

    for (var i = 0; i < 2; i++) {
        $('select#name_' + i).on('change', {
            index: i
        }, function (e) {
            name = $(this).val();
            if ($.trim(name) != '') {
                $.post('ajax/name.php', {
                    name: name
                }, function (data) {
                    $('input#name-id_' + e.data.index).val(data);
                });
    
                $.post('ajax/location.php', {
                    name: name
                }, function (data) {
                    $('input#location_' + e.data.index).val(data);
                });
    
            }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试