在jquery中获取选择框名称

时间:2016-11-02 10:11:26

标签: javascript jquery

我有几个选择框,我想知道哪个选择框被触发。

Html代码:

<form id="property-filters" method="post">
    <select name="sel1">
    <option value="1">1</option>
    <option value="2">2</option>
    </select>

    <select name="sel2">
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
</form>

Jquery代码:

$(document).on('change', '#property-filters input, #property-filters select', function () 
{
//Get the name of the select box.
}

非常感谢任何帮助。提前谢谢。

3 个答案:

答案 0 :(得分:2)

检查事件触发的dom对象的name属性。

$(document).on('change', '#property-filters input, #property-filters select',function () {
   if(this.name == 'sel1'){
       // do the rest
   }else if(this.name == 'sel2'){
       // do the rest    
   }
})

答案 1 :(得分:1)

使用attr()

$(this).attr('name');

答案 2 :(得分:0)

你可以做到

$('#property-filters select').change(function () {
   alert($(this).prop('name'));
});

检查JSFiddle