简体   繁体   English

从数据库表获取价值进入选择框

[英]to get value from database table into select box

im creating a combo box which gets value from mysql database table. 即时通讯创建一个从MySQL数据库表获取价值的组合框。 here is a sample code which im implementing but it will not populates selectbox values. 这是即时执行的示例代码,但不会填充选择框值。

 mydata +='<div class="content  nodisplay"><div class="row"><div class="label" style="font-size:22px;">Subject</div><div class="data">
<select id="fillsubject" name="fillsubject">';
$.post(document.URL,qstring,function(data){

        var subjects =  $(data).filter('.subjects');

        $.each(subjects,function(index,value){
            var subid = $(this).attr('id');
            var subname = $(this).text();
            mydata += "<option value='"+subid+"'>"+subname+"</option>";
            //mydata += "<option value='english'>english</option>";
        });

});
mydata +='</select></div></div></div>';     
var mydata +='<div class="content  nodisplay"><div class="row"><div class="label" style="font-size:22px;">Subject</div><div class="data"><select id="fillsubject" name="fillsubject"></select></div></div></div>';
$.post(document.URL,qstring,function(data){

        var subjects =  $(data).filter('.subjects');
        var subopts = '';
        $.each(subjects,function(index,value){
            var subid = $(this).attr('id');
            var subname = $(this).text();
            subopts += "<option value='"+subid+"'>"+subname+"</option>";
        });
        //after the loop is over building your <options/> inject the html
        $('#fillsubject').html(subopts);
});

You are splitting the <select></select> that's not necessary. 您正在拆分<select></select>不必要的。 You can do it as above. 您可以按照上面的步骤进行操作。

How does the code sample fit into the rest of your source code? 该代码示例如何适合您的其余源代码?

Rather than using 而不是使用

mydata += "<option value='"+subid+"'>"+subname+"</option>";

in your .each function, you would be better off using 在您的.each函数中,最好使用

$("#fillsubject").append("<option value='"+subid+"'>"+subname+"</option>");

I don't know what you're doing with mydata after the code sample you post, but since .post is asynchronous, mydata may have already been displayed to the user when your js gets the reply from your server. 在发布代码示例后,我不知道您对mydata做什么,但是由于.post是异步的,因此当您的js从服务器获取答复时, mydata可能已经显示给用户。 If this is the case, then the suggestion above will work as expected. 如果是这种情况,那么上面的建议将按预期工作。

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

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