简体   繁体   English

如何使用jQuery检测下拉菜单中选项内容的变化?

[英]How to detect option content change in dropdown using jquery?

I have a dropdown field, options of which will be changed dynamically. 我有一个下拉字段,其选项将动态更改。 Is there a way to capture the event, whenever the set of options inside the dropdown is changed? 每当下拉菜单中的选项集发生更改时,是否都可以捕获事件?

I do not want to capture the option change ie 我不想捕获选项更改,即

$(".dropdown").on("contentChange", function(){
    //do something
})

Rather, I want to capture the event, when the set of options inside the dropdown gets modified. 相反,当下拉列表中的选项集被修改时,我想捕获事件。 How can i attach a handler, which will get trigger functions when option set changes. 我如何附加处理程序,当选项集更改时将获得触发器功能。

It is possible to watch DOM changes, in newset DOM specification we have MutationObserver to observe DOM. 可以观察DOM的变化,在新的DOM规范中,我们有MutationObserver来观察DOM。 Working exampe 工作范例

 MutationObserver = window.MutationObserver || window.WebKitMutationObserver; //create observer var observer = new MutationObserver(function(mutations, observer) { console.log("Options have been changed"); }); //set to observe childs ( options ) observer.observe(document.querySelector(".dropdown"), { subtree: true, childList:true, attributes:true }); //test change $('button').on("click",function(){ $(".dropdown").prepend("<option>New added option</option>"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="dropdown"> <option>First</option> <option>Second</option> <option>Third</option> <option>Fourth</option> </select> <button>Test button options change</button> 

You can use also event DOMSubtreeModified . 您还可以使用事件DOMSubtreeModified Event work on major browsers but is DEPRECATED . 事件可在主要浏览器上使用,但已弃用 Working example: 工作示例:

 //add event $(".dropdown").on("DOMSubtreeModified", function(e){ console.log('Options have been changed'); }); //change option example after button click $('button').on("click",function(){ $(".dropdown").prepend("<option>New added option</option>"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="dropdown"> <option>First</option> <option>Second</option> <option>Third</option> <option>Fourth</option> </select> <button>Test button options change</button> 

I added some example options modification after button click. 单击按钮后,我添加了一些示例选项修改。 Event will be called on any DOM change inside select . select内的任何DOM更改都将调用事件。

You can add your custom event to DOM document and simulte it when content change using jquery .trigger() . 您可以将自定义事件添加到DOM文档中,并在使用jquery .trigger()更改内容时对其进行.trigger()

 $("button").click(function(){ $("select > option:first").text("A1").trigger("contentChange"); }); $("select > option").on("contentChange", function(){ console.log("Content changed"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Change content</button> <select> <option>A</option> <option>B</option> <option>C</option> </select> 

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

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