【问题标题】:jquery chosen update hidden input on new option addedjquery 选择更新隐藏输入添加新选项
【发布时间】:2018-07-02 05:25:41
【问题描述】:

我正在使用 jQuery Chosen 插件并且能够动态添加新选项

$('#element').chosen({
    create_option: true,
    // persistent_create_option decides if you can add any term, even if part
    // of the term is also found, or only unique, not overlapping terms
    persistent_create_option: true,
    // with the skip_no_results option you can disable the 'No results match..' 
    // message, which is somewhat redundant when option adding is enabled
    skip_no_results: true,
    create_option_text: 'Add option'
});

我需要的是一种仅在单击添加选项链接时更新隐藏字段的方法。

在 Chrome 中检查代码后,我可以看到添加了以下 html:

<li class="create-option active-result"><a>Add option</a>: "dsgdsgsdgdg"</li>

我尝试使用点击事件来定位这些类,但它不起作用

$('.create-option.active-result').click(function() {
    alert('fsgsdgdsgd');
});

有没有办法定位此链接或检查选择列表的长度是否发生了变化?

【问题讨论】:

  • 元素是通过 DOM 加载还是稍后动态添加?
  • 有一个输入框,如果您输入的值不在原始选择中,它会提供添加到列表中的选项,以便将选项附加到选择中
  • 这就解释了问题所在。当您在动态元素上绑定事件时,您必须以不同的方式进行。

标签: jquery options jquery-chosen


【解决方案1】:

您想将事件绑定到动态添加的元素。

看到这个问题:Event binding on dynamically created elements?

如上所述,您必须致电:

$("#myList").on("click", ".create-option.active-result", function() {
    alert(this.text);
});

【讨论】:

  • @PeterGriffin 好吧。然后请提供一个工作小提琴或代码-sn-p。否则无法为您提供帮助。
猜你喜欢
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 2017-06-09
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
相关资源
最近更新 更多