简体   繁体   English

jQuery追加选择选项-选择不起作用

[英]jQuery append select options - selected not working

I am building a select box by grabbing the options using ajax. 我通过使用ajax抓取选项来构建选择框。 I am then checking if the value of each options is the same as the current selected, and if so add the 'selected' attribute. 然后,我要检查每个选项的值是否与当前选定的值相同,如果是,则添加“ selected”属性。 The 'selected' attribute is being added fine but that option does not show as selected. 可以很好地添加'selected'属性,但是该选项不会显示为选中状态。

This is how I'm appending the options to the select.. 这就是我将选项附加到选择项的方式。

for (var i=0; i<client_jobs.length; i++) {
    if(current_job == client_jobs[i].id){
        sel.append('<option value="' + client_jobs[i].id + '" selected="selected">' + client_jobs[i].label + '</option>');
    }else{
        sel.append('<option value="' + client_jobs[i].id + '">' + client_jobs[i].label + '</option>');  
    }   
}

The correct option is seen like this: 正确的选项如下所示:

<option value="5" selected="selected">fifth test job</option>

But it is not actually selected, it's always the first options which is seen. 但是实际上并没有选择它,它始终是显示的第一个选项。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Thanks. 谢谢。

Your code works perfectly well, 您的代码运行良好,
Try to wrap your code into a DOM ready function 尝试将您的代码包装到DOM ready函数中

 jQuery(function( $ ){ // Try to use this var sel = $("select"); var current_job = 1; // set b as selected var client_jobs = [ {id:0, label:"a"}, {id:1, label:"b"}, {id:2, label:"c"} ]; for (var i=0; i<client_jobs.length; i++) { var cj = client_jobs[i]; if(current_job == cj.id){ sel.append('<option value="' + cj.id + '" selected="selected">' + cj.label + '</option>'); }else{ sel.append('<option value="' + cj.id + '">' + cj.label + '</option>'); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select></select> 

PS: try to avoid using .append() inside a for loop. PS:请尽量避免在for循环中使用.append()
Instead, concatenate your strings and than after the loop append only once: DEMO 而是连接字符串,然后在循环之后仅追加一次: DEMO

Assuming your client_jobs looks like: 假设您的client_jobs看起来像:

var client_jobs = [
    {id:0, label:"first"},
    {id:1, label:"second"},
    {id:2, label:"third"},
    {id:3, label:"fourth"}
];

and you'd like to set one of the options selections, let's say: 并且您想要设置选项之一,比如说:

var current_job = 2;

You could do the following with no if etc. 您可以执行以下操作, if没有, if否。

 var client_jobs = [ {id:0, label:"first"}, {id:1, label:"second"}, {id:2, label:"third"}, {id:3, label:"fourth"} ]; var current_job = 2; var $options = $(); for (var i=0, len=client_jobs.length; i<len; i++) { $options = $options.add( $("<option/>", { 'value': client_jobs[i].id, 'text': client_jobs[i].label }) ); } $("select").append($options).val(current_job); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select></select> 

Tip: Do not append within a loop (ever!) 提示:请勿在循环中附加(永远!)

Hope that helps! 希望有帮助!

for (var i=0; i<client_jobs.length; i++) {
    if(current_job == client_jobs[i].id){
        var option = document.createElement("option");
        option.value = client_jobs[i].id;
        option.setAttribute("selected", "selected");
        option.appendChild(document.createTextNode(client_jobs[i].label));
        sel.append(option);
    }else{

        var option = document.createElement("option");
        option.value = client_jobs[i].id;
        option.appendChild(document.createTextNode(client_jobs[i].label));
        sel.appendChild(option);

    }   
}
var client_jobs = {"0": {id: "job1",label: "JOB1"},"1": {id: "job2",        label: "JOB2"}};

var current_job = "job2";
for (i in client_jobs) {

  if(current_job == client_jobs[i].id){

    $('#select').append('<option value="' + client_jobs[i].id + '" selected="selected">' + client_jobs[i].label + '</option>');

  }else{

    $('#select').append('<option value="' + client_jobs[i].id + '">' + client_jobs[i].label + '</option>');  

   } 

 }

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

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