简体   繁体   English

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

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

I have a drop box (Using the jQuery Autocomplete widget) which is populated using an AJAX call to a PHP file called filter items.php . 我有一个下拉框(使用jQuery Autocomplete小部件),该框使用AJAX调用填充到名为filter items.php的PHP文件中。 I want to be able to keep track of what items the user has selected so that if they were to go to another page, their selections are retained when they return. 我希望能够跟踪用户选择了哪些项目,以便如果他们要转到另一个页面,则他们返回时将保留他们的选择。

I'm attempting to accomplish this by populating a table that holds the item's ID's temporarily as they make their selections. 我正在尝试通过填充一个在选择时临时保存项目ID的表来完成此操作。 When they go back to the page it will make another AJAX call to a PHP file called 'check basket.php' which will return the list of items currently selected. 当他们返回页面时,它将对名为“ check basket.php”的PHP文件进行另一个AJAX调用,该文件将返回当前所选项目的列表。 It will then compare items from both lists as it generates the drop down list, when there is a match I add prop('selected', true) but it does not seem to work? 然后,当生成匹配项时,它将比较两个列表中的项目,当有匹配项时,我添加了prop('selected', true)但似乎不起作用?

Here's the code: 这是代码:

//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++;
              }
            }
          }
        })
      }
    });
  }
});

I've realised what I've done wrong here, Prop('selected',true) is working fine, its just that the actual content of the list was not generated before the Jquery autocomplete code did its thing. 我意识到我在这里做错了什么,Prop('selected',true)正常工作,只是在Jquery自动完成代码执行其操作之前未生成列表的实际内容。 I changed the the setting of the AJAX functions that generate the list to be Asynchronous and that did the trick! 我将生成列表的AJAX函数的设置更改为“异步”,并且成功了!

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

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