简体   繁体   English

在jquery中获取选择框名称

[英]Get select box name in jquery

I have couple of select box and I would like to know which select box is triggered. 我有几个选择框,我想知道哪个选择框被触发。

Html code: 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 code: Jquery代码:

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

Any help is highly appreciated. 任何帮助都非常感谢。 Thanks in advance. 提前致谢。

Check the name property of the event fired dom object. 检查事件触发的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    
   }
})

使用attr()

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

You can do 你可以做

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

Check JSFiddle 检查JSFiddle

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

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