2016-09-29 79 views
0

我覺得這不是選擇數據的正確方法:縮短jQuery的多個孩子選擇

$(document).on('blur', 'td', function(){ 
cID = $(this).children('div.cColor') 
       .children('select.dropdown.cColor_dropdown') 
       .children("option") 
       .filter(":selected") 
       .data("cid"); 
} 

有縮短這個繁瑣的選擇的任何可能的方式? 編輯:HTML

<tr> 
     <td class="cID"> 
      <div class="cColor"> 
       <select class="dropdown cColor_dropdown" > 
       <option data-cID="21" value="client_name">client_name</option> 
       <option data-cID="22" value="2_client_name">2client_name</option> 
       </select> 
      </div> 
     </td> 
</tr> 
<tr> 
    <td class="cID"> 
       <div class="cColor"> 
        <select class="dropdown cColor_dropdown" > 
        <option data-cID="21" value="client_name">client_name</option> 
        <option data-cID="22" value="2_client_name">2client_name</option> 
        </select> 
       </div> 
      </td> 
</tr> 

回答

1

,如果你提供的標記(HTML)這將是更有幫助。 有很多選擇:

cID = $(this).find('div.cColor>select.dropdown.cColor_dropdown>option:selected').data("cid"); 
cID = $(this).find('option:selected').data("cid"); // if there is only one select 
cID = $(this).find('select#select-id>option:selected').data("cid"); // select with id 
cID = $(this).find('select#select-id option:selected').data("cid"); // if select specified, no need for > 
+0

的第一個建議的作品和有更多的則是一個選擇不進行身份 – Atis