2016-09-29 86 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