简体   繁体   English

从父数据属性值中选择子元素

[英]Select child element from parent data attributes value

I have this kind of html blocks: 我有这种html块:

<div data-target="TargeID" data-action="toggle" data-trigger="Yes">
    ...

    <ul>
        <li>
            <label class="radio">
            <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_0" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl01$rbAnswer">
            Yes             
            </label>
        </li>

        <li>
            <label class="radio">
            <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_1" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl02$rbAnswer">
            No              
            </label>
        </li>

        <li>
            <label class="radio">
            <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_2" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl03$rbAnswer">
            maybe               
            </label>
        </li>
    </ul>
</div>

I need to select every input inside elements with data-action="toggle" 我需要使用data-action="toggle"选择元素内的每个输入

and Im using this jQuery selector: $('[data-action="toggleQuestions"] :input'); 我使用这个jQuery选择器: $('[data-action="toggleQuestions"] :input');

but I need to select those input which text value is equal to the parent data-trigger value. 但我需要选择那些text值等于父data-trigger值的输入。

Is it possible directly with a selector? 是否可以直接使用选择器?

The logic is too complex to put in to a single selector. 逻辑太复杂,无法放入单个选择器。 Instead you can use filter() to find the :input elements, then match the text() of their parent label to the data-trigger value on the container. 相反,您可以使用filter()来查找:input元素,然后将其父labeltext()与容器上的data-trigger值进行匹配。 Try this: 尝试这个:

 var $container = $('[data-action="toggle"]'); var $input = $container.find(':input').filter(function() { return $(this).closest('label').text().trim() == $container.data('trigger'); }) $input.prop('checked', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-target="TargeID" data-action="toggle" data-trigger="Yes"> <ul> <li> <label class="radio"> <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_0" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl01$rbAnswer"> Yes </label> </li> <li> <label class="radio"> <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_1" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl02$rbAnswer"> No </label> </li> <li> <label class="radio"> <input id="cpMainContent_ctl24_rptAnswers_rbAnswer_2" type="radio" name="cpMainContent_ctl24" value="ctl00$cpMainContent$ctl24$rptAnswers$ctl03$rbAnswer"> maybe </label> </li> </ul> </div> 

Use the below code 使用以下代码

$('[data-action="toggle"]').each(function() {
    var triggerValue = $(this).attr("data-trigger");

    $(this).find('input[type=radio]').each(function() {
        if ($(this).parent().text().trim() === triggerValue) {
            $(this).prop("checked", true);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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