简体   繁体   中英

Get index of an dynamic element in JQuery

I have a DOM like this

<div class="table">
<div class="tr">
    <div class="td"> 
        <select class="selCat" name="selCat[]">
          <option>1</option>
          <option>2</option>
       </select>
    </div>
    <div class="td"> 
        <p class="sub">text 1</p>
    </div>
 </div>   
<div class="tr">
    <div class="td"> 
        <select class="selCat" name="selCat[]">
          <option>3</option>
          <option>4</option>
       </select>
    </div>
    <div class="td"> 
        <p class="sub">text 2</p>
    </div>
 </div>   
<div class="tr">
    <div class="td"> 
        <select class="selCat" name="selCat[]">
          <option>5</option>
          <option>6</option>
       </select>
    </div>
    <div class="td"> 
        <p class="sub">text 3</p>
    </div>
 </div>   

How can I get the index of particular "selCat" on a change event ? I need to know the index because I need to assign specific value based on previous dropdown value to "sub" class of particular index.

This is what I did:

$(".selCat").change(function(){
    alert($(this).index(".selCat"));
});

Here is the JSFiddle demo

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