简体   繁体   中英

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:

<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:

$(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.

$(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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