jQuery如何区分更改了哪个“ <select id =” value / ID?

时间:2019-12-13 01:20:57

标签: javascript jquery json html-select onchange

我的主要问题是,如何区分哪个下拉框已更改? 本质上,我具有下拉框/组合框的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")
    }
});

1 个答案:

答案 0 :(得分:0)

您可以获得引发事件的元素的ID。例如:

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