繁体   English   中英

jQuery 如何区分“

[英]jQuery How to differentiate which "<select id=" value/ID was changed?

我的主要问题是,如何区分更改了哪个下拉框? 基本上我有这个下拉框/组合框的 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') {
  //...
}

暂无
暂无

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

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