简体   繁体   English

选择插件 - 删除特定选项的“x”按钮(删除选项) after.trigger("chosen:updated")

[英]Chosen plugin - remove "x" button (remove option) for specific options after .trigger("chosen:updated")

Using: jQuery 3.5.1使用:jQuery 3.5.1

Basically I have multiple DDLists, rendered by server(ASP MVC app) with selected=true or false options, that I attach chosen to them, the selected options (that are rendered by server) should not be removed so I removed "x" button from them.基本上我有多个 DDLists,由服务器(ASP MVC 应用程序)呈现,带有selected=truefalse选项,我选择了它们,不应删除selected选项(由服务器呈现)所以我删除了“x”按钮从他们。 What I want now is this:我现在想要的是这样的:

  • When I choose an option from one of these DDLs, I want that option to not be available for rest of DDLs, and if I remove it -> to be available again for all DDLs -> this I already managed to get to work (with the code below) by using .trigger("chosen:updated")当我从其中一个 DDL 中选择一个选项时,我希望该选项对 rest 个 DDL 不可用,如果我删除它 -> 再次对所有 DDL 可用 -> 我已经设法开始工作(使用下面的代码)通过使用.trigger("chosen:updated")
  • Doing .trigger("chosen:updated") , updates all my DDLs, which implicit add "x" button to all selected options执行.trigger("chosen:updated") ,更新我所有的 DDL,这隐含地将“x”按钮添加到所有选定的选项
  • Now what I need is a way of prevent adding "x" button to those options that were initially rendered by server -> I've tried to save them in search_choices but at that moment they doesn't contain "x" button and after .trigger("chosen:updated") , DOM is changed and search_choices contains an old version of those elements.现在我需要的是一种防止将“x”按钮添加到最初由服务器呈现的那些选项的方法 - >我试图将它们保存在search_choices中但在那一刻它们不包含“x”按钮和之后.trigger("chosen:updated") ,DOM 已更改并且search_choices包含这些元素的旧版本。

There was a solution that is deprecated now by using ".selector" -> which gives you a updated version of DOM elements有一个解决方案现在已弃用,方法是使用“.selector” -> 它为您提供 DOM 元素的更新版本

How can I overcome this issue?我怎样才能克服这个问题? Thanks in advance!提前致谢!

   var search_choices = $("#MyFieldset").find(".search-choice");

   $("#step_5").on("change", ".ChosenDDLs", function (ev, crtSelectedOptionVal) {
    
    var all_ChosenDDLs = $(".ChosenDDLs");

    if (crtSelectedOptionVal.deselected) {
        all_ChosenDDLs .not(this).each(function (index, elem) {
            $(elem).children("option[value=" + crtSelectedOptionVal.deselected + "]")
                   .prop("disabled", false)
                   .trigger("chosen:updated");
        });
    }
    else if (crtSelectedOptionVal.selected) {
        all_ChosenDDLs .not(this).each(function (index, elem) {
            $(elem).children("option[value=" + crtSelectedOptionVal.selected + "]")
                   .prop("disabled", true)
                   .trigger("chosen:updated");
        });
    }

///search_choices here does not contain updated DOM elemets, so code below doesn't work
    if (search_choices.length > 0) {
        search_choices.each(function (index, elem) {
            $(this).find(".search-choice-close").remove();
        });
    }        
});

So what I actually did was to render those options as disabled (from server), so no "x" button was attached to them, and also force jQuery validation to not ignore disabled options: :disabled:not(option)所以我实际上做的是将这些选项呈现为禁用(从服务器),因此没有“x”按钮附加到它们,并且还强制 jQuery 验证不忽略禁用的选项:disabled:not(option)

$("#form").data("validator").settings.ignore = ":disabled:not(option)";

Also with chosen you need to not ignore hidden select , so validation can work (after reparsing Validation: $.validator.unobtrusive.parse($("#form")) )同样选择你需要不忽略隐藏select ,所以验证可以工作(重新解析验证后: $.validator.unobtrusive.parse($("#form"))

$("#form").data("validator").settings.ignore = ":hidden:not(" + selectClassnameToIgnore +")";

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

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