weixin_33705053 2018-05-20 23:37 采纳率: 0%
浏览 13

使用AJAX的下拉菜单

I have installed a dropdown menu (in a php site) that makes use of Ajax functionality to populate the dropdown list. It works ok, except that the list only populates if you tab into the dropdown button. If you click on the button it needs two clicks to populate (this tends to throw people in a major way).

The function is called from a focus event. I have tried click, onclick, load, onload and others, but nothing works.

The form code reads;

echo "<script>

    $(document).ready(function(){

        $('.sel_field').focus(function(){


            $.ajax({
                url: 'GetClient.php',
                type: 'post',
                dataType: 'json',
                success:function(response){

                    var len = response.length;

                    $('#sel_user').empty();
                    for( var i = 0; i<len; i++){
                        var id = response[i]['id'];
                        var name2 = response[i]['username'];
                        var name = response[i]['name'];
                        var mat = response[i]['Matter'];

                        $('#sel_user').append('<option value='+id+'> ClientID: '+id+' -  Name:   '+name+' : '+mat+'</option>');


                    }
                }
            });
        });

    });


</script>";

echo "<tr>";
echo "<td>";
echo "Client ID <span style='font-size:10px'>(Press tab to enter)</span>";
echo "</td>";
echo "<td>";
echo "<select  name='clientID' style='width:460px' class='form-control sel_field' id='sel_user' >
        <option value='0'> - Make A Selection -</option>
    </select>";
echo "</td>";
echo "</tr>";
  • 写回答

1条回答 默认 最新

  • weixin_33696822 2018-05-21 01:06
    关注

    It's hard to help you without knowing what you're expecting and what you're actually getting. I've made a snippet based on your code, replacing the ajax call with a setTimeout function. It's doing something... how close/far is it to what you're experiencing and what exactly are you expecting?

    -- EDIT: maybe the delay for the load is throwing people? I've added a line to change the select text to "loading" while waiting for the ajax to finish what it's doing.

    $(document).ready(function(){
        $('.sel_field').focus(function(){
             $('#sel_user').html('<option value="0">Loading...</option>');
             setTimeout(function(){
    
               var len = 2;
    
               $('#sel_user').empty();
               for( var i = 0; i<len; i++){
                   var id = 1;
                   var name2 = 'name2-'+i;
                   var name = 'name-'+i;
                   var mat = 'mat'+i;
    
                   $('#sel_user').append('<option value='+id+'> ClientID: '+id+' -  Name:   '+name+' : '+mat+'</option>');
               }
            }, 1000);
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <tr>
    <td>
    Client ID <span style='font-size:10px'>(Press tab to enter)</span>
    </td>
    <td>
    <select  name='clientID' style='width:460px' class='form-control sel_field' id='sel_user' >
            <option value='0'> - Make A Selection -</option>
        </select>
    </td>
    </tr>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊