猿问

如何在选择中使用 jQuery 将选项的 ID 动态更改为目标

使用 CMS,我创建了一个事件页面,我们可以将其称为蓝图,因为它将被复制以创建子事件,并且我在蓝图中所做的任何更改都可以下推到所有子事件。


在我的蓝图中,我有一个调查,其中有一个下拉问题要求用户选择他们的部门。


根据选择的部门,下一个问题过滤该部门可用的区域列表。


我试图定位一个由系统分配的<option></option>元素,id但id每个复制的事件都不同。


  function showHideTEAM(selectedValue){

    if (selectedValue != "NORTH" && selectedValue != "NORD" && selectedValue != "TEAM CANADA") {

      $("#1684_53515_8_169135 > option").each(function() {

        if($(this).val() == "TEAM CANADA") {

          $(this).remove();

        }

      });

    }

    if (selectedValue == "TEAM CANADA") {

      $("#1684_53515_8_169135 > option").each(function() {

        if($(this).val() != "TEAM CANADA") {

          $(this).remove();

        }

      });

    } 

  }

  if ($("#1684_53515_7_169134").length > 0) {

    $("#1684_53515_7_169134").on("change", function(event) {showHideMELLON($(this).val());});

  }

打算发生的是


如果用户id="1684_53515_7_169134"在下一个问题的过滤下拉列表 ( id="1684_53515_8_169135") 中选择 North(如果他们使用英语)或 Nord(如果使用法语),则会将TEAM CANADA 添加到列表中。到目前为止......这似乎有效,尽管复制和id改变,我不知道为什么。


不起作用的是,如果id="1684_53515_7_169134"您选择 TEAM CANADA,下一个问题id="1684_53515_8_169135"应该删除所有选项,并且只将 TEAM CANADA 作为选项。


我知道在那里有两次加拿大队是没有意义的,但这是必需的。


一些附加信息,所有事件页面共享一个页面包装器,但事件页面彼此分开。注册流程(此调查出现的地方)在活动页面之间也是分开的。那么为什么系统会为我不知道的每个事件的相同字段为每个事件生成一个新的 ID 号。


喵喔喔
浏览 176回答 1
1回答

Cats萌萌

我想到了。找到了 jQuery [attribute*=value] 选择器。用它来找到ids 中的公共数,所以前任&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedValue == "TEAM CANADA") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("select[name*='_8_'] > option").each(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($(this).val().toUpperCase() != "TEAM CANADA") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答