jQuery 如何区分哪个"<select id="值/ID 被更改了?



我的主要问题是,如何区分哪个下拉框被更改了? 本质上,我有这个下拉框/组合框的HTML代码,它由JS中的JSON文件填充。

<select id="filterCountry">
<option value="0">All Countries</option>
</select>
<select id="filterBrowser">
<option value="0">All Browsers</option>
</select>
<select id="filterOS">
<option value="0">All Operating System</option>
</select>

我的jQuery代码现在与此类似。

$("#filterBrowser, #filterOS, #filterCountry").change(function(e){
alert("Something has been changed "  + this.value);
// Ugly pseudocode but something along the lines of...
if(("#filterBrowser").change) {
console.log("the browser drop down was changed");
} else if (("#filterOS").change) {
console.log("The OS drop down changed");
} else if(("#filterCountry").change) {
console.log("The country drop down was changed")
}
});

您可以获取引发事件的元素的 id。 例如:

if(this.id === 'filterBrowser') {
//...
}

相关内容

最新更新