【问题标题】:Shorten jQuery multiple children selector缩短 jQuery 多子选择器
【发布时间】:2016-09-29 11:35:48
【问题描述】:

我觉得这不是选择数据的正确方式:

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

【问题讨论】:

    标签: jquery jquery-selectors children


    【解决方案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 >
    

    【讨论】:

    • 第一个建议有效,因为没有 id 的选择不止一个
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2010-10-24
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多