繁体   English   中英

Prop('selected',true)无法预选下拉列表项目

[英]Prop('selected',true) Not working to pre-select drop list item

我有一个下拉框(使用jQuery Autocomplete小部件),该框使用AJAX调用填充到名为filter items.php的PHP文件中。 我希望能够跟踪用户选择了哪些项目,以便如果他们要转到另一个页面,则他们返回时将保留他们的选择。

我正在尝试通过填充一个在选择时临时保存项目ID的表来完成此操作。 当他们返回页面时,它将对名为“ check basket.php”的PHP文件进行另一个AJAX调用,该文件将返回当前所选项目的列表。 然后,当生成匹配项时,它将比较两个列表中的项目,当有匹配项时,我添加了prop('selected', true)但似乎不起作用?

这是代码:

//Check for items in Basket
$.ajax({
  url: "check basket.php",
  dataType: "json",
  success: function(datas) {
    var s = 0;
    var cbox = [];
    var itemid = [];
    var quant = [];
    $.each(datas, function(index, selected) {
      while (selected.cbox[s]) {
        cbox[s] = selected.cbox[s];
        itemid[s] = selected.item_id[s];
        quant[s] = selected.quant[s];
        s++;
      }
    })

    //BUILD LIST
    $.ajax({
      url: "filter items.php?type=0",
      dataType: "json",
      success: function(data) {
        $.each(data, function(index, publisher) {
          for (r = 1; r < 21; r++) {
            j = 1;
            while (publisher.item[j]) {
              for (sc = 0; sc < cbox.length; sc++) //Compare items in basket to pre-select
              {
                if (r == cbox[sc] && publisher.item_id[j] == itemid[sc]) {
                  alert(publisher.item[j] + " is in Basket for cbox " + cbox[sc] + " and J is " + j);
                  $('#' + r + 'combobox').append($('<option>', {
                    value: publisher.item_id[j]
                  }).text(publisher.item[j]).prop('selected', true));
                } else
                  $('#' + r + 'combobox').append($('<option>', {
                    value: publisher.item_id[j]
                  }).text(publisher.item[j]));
                j++;
              }
            }
          }
        })
      }
    });
  }
});

我意识到我在这里做错了什么,Prop('selected',true)正常工作,只是在Jquery自动完成代码执行其操作之前未生成列表的实际内容。 我将生成列表的AJAX函数的设置更改为“异步”,并且成功了!

暂无
暂无

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

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