【问题标题】:How to reload jQuery multiselect in a drowdown list when the selection of another dropdown changes当另一个下拉列表的选择发生变化时,如何在下拉列表中重新加载jQuery多选
【发布时间】:2018-09-29 04:19:55
【问题描述】:

我已经尝试了这个论坛中提出的一些解决方案,但它们没有奏效,所以我认为我在某个地方遗漏了一些东西。

我在两个下拉列表(ProductCategories 和 Products)上使用 Patrick Springstubbe jQuery 多选。

我希望产品列表根据 ProductCategories 多选中的选择进行更改。

我使用的代码如下:-

	$("#ProductCategory").change(

		function () {

		$("#leftlist").empty();

		$("#ProductCategory").children("option:selected").each(function () {

			ResetLeftList(($(this).val()));		

		});
		
		$('#leftlist').multiselect( 'reload' );

	});

function ResetLeftList(ProductCategoryID) {

  //Get list of food for categoryID

  $.ajax

  ({

    url: "http://example.com/index.php?id=18",
    data: {
      PostProductCategoryID: ProductCategoryID
    },
    method: "post",
    success: function(result) {

      var trimmedresult = result.substring(3, result.length - 4);
      var newOptions = trimmedresult.split(";");

      for (i = 0; i < newOptions.length; i++) {

        var option = newOptions[i].split(",");

        $("#leftlist").append($("<option></option>").attr("value", option[0]).text(option[1]));

      }

    }

  });


}

问题:- 产品多选不更新

观察:- 在网络浏览器上使用开发人员工具,我可以看到产品列表中的选项列表正在按预期更改。 如果我删除$("#leftlist").empty();,产品多列表会根据之前从类别列表中选择的选项进行更新。 $('#leftlist').multiselect( 'reload' ) 似乎在产品列表中的选项更改之前触发。

【问题讨论】:

  • 请尝试将两个 sn-ps 合并到一个工作中,包括一些示例 html,以便我们能够重现该场景并提出可能的解决方案
  • 要重现该场景,您可以转到我的开发站点的 url fansip.wowislandcharter.com 对于那些稍后查看此帖子的人请注意,此链接将在开发完成后更改。跨度>

标签: jquery jquery-multiselect


【解决方案1】:

你是对的“$('#leftlist').multiselect('reload') 在产品列表中的选项更改之前触发。”。

jQuery.ajax 执行异步 HTTP (Ajax) 请求。

这意味着代码将在调用 $.ajax() 之后继续执行。

您可以使用jqXHR Object returned by $.ajax() 并将重新加载代码放在将在请求完成时执行的回调中。

返回 jqXHR 对象:

function ResetLeftList(ProductCategoryID) {
    return $.ajax

等待所有 ajax 完成

 var requests = new Array();

 $("#ProductCategory").children("option:selected").each(function () {

     requests.push(ResetLeftList(($(this).val())));     

 });

 $.when.apply($, requests).done(function( x ) {
     $('#leftlist').multiselect( 'reload' );
 });

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多