简体   繁体   English

列表填充后,在动态填充的下拉列表中选择一个值

[英]Selecting a value on a dynamically populated dropdown list after the list has been populated

So I have a problem where my code tries to select a value on the dropdown list before the list is populated. 所以我有一个问题,我的代码尝试在填充列表之前在下拉列表中选择一个值。 Basically it calls a javascript function that does an AJAX post to get the dropdown values from php. 基本上,它调用一个执行AJAX发布的javascript函数,以从php获取下拉列表值。 Then its supposed to select a value on the list, however it does this before the list is populated, so it doesn't find the value. 然后,它应该在列表上选择一个值,但是它是在填充列表之前执行的,因此找不到该值。 Any idea on how to fix this? 关于如何解决此问题的任何想法?

Heres my code 这是我的代码

This is where I get the values for the dropdown list 这是我获取下拉列表的值的地方

function getProjects(id, proj_select_class)
{
    custID = id.options[id.selectedIndex].value;

    $.ajax({
        type: "POST",
        url: "index.php/home/projectlist",
        data: {custID : custID},
        dataType: "json",

        success:function (result){                
            var ddl = $(proj_select_class);

            ddl.children('option:not(:first)').remove();              
            for (var key in result) {
                if (result.hasOwnProperty(key)) {
                    ddl.append('<option value=' + key + '>' +  result[key] + '</option>');                              
                }
            }                         
        }

    }); 
}

And heres where I set the values. 我在这里设置值。 AddNew() adds a new row to my table. AddNew()向我的表添加一个新行。 This is also inside an ajax call. 这也在ajax调用中。

for (var row in result) {
    AddNew();                                                                   

    client_field = document.getElementById('clients'+id);
    project_field = document.getElementById('projects'+id); 

    client_value = $.trim(result[row].client_id);
    project_value = $.trim(result[row].project_id);     

    //set client                        
    client_field.value = client_value;                      

    getProjects(client_field, project_field, client_value);
    project_field.value = project_value;        
}

Maybe try using a custom event binding to know when your code should fetch the value from the list. 也许尝试使用自定义事件绑定来了解您的代码何时应从列表中获取值。 To bind to a custom event, you would do something like: 要绑定到自定义事件,您可以执行以下操作:

$(document).bind("listpopulated", function(){ /*find value, call AddNew() */ });

and in your ajax success function trigger the "listpopulated" event like so: 并在您的ajax成功函数中触发“ listpopulated”事件,如下所示:

$(document).trigger("listpopulated");

References: 参考文献:

http://api.jquery.com/bind/ http://api.jquery.com/bind/

http://api.jquery.com/trigger/ http://api.jquery.com/trigger/

Fixed it by waiting til the ajax finished running by doing this 通过等待直到ajax完成运行来修复它

$(document).ajaxComplete(function(){ set_values(result); });

set_values is another function that I just loops through all my results and sets all the dropdown values set_values是另一个函数,我仅循环遍历所有结果并设置所有下拉值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM