如何使用具有输入属性值的选择器来访问HTML元素?

时间:2014-07-23 02:59:35

标签: jquery

例如,在点击带有类a-choice1

的div时使用此jQuery函数
$('.a-choice1').click(function () {
    if ($(this).children('input').prop('value') == 0){
        $(this).siblings().children('input[value=1]').addClass('highlight_selected_right');
    }
});

在以下html代码中到达值= 1的input元素:

<div class="answer-container">
    <div class="a-choice1">
        <input type="radio" name="qa1" value="0" />
        <label for="btn1"></label>
        <span>The</span>    
    </div>

    <div class="a-choice1">
        <input type="radio" name="qa1" value="0" />
        <label for="btn2"></label>
        <span>Albus</span>
    </div>

    <div class="a-choice1">
        <input type="radio" name="qa1" value="0" />
        <label for="btn3"></label>
        <span>Number</span>
    </div>

    <div class="a-choice1">
        <input type="radio" name="qa1" value="1" id="correct"/>
        <label for="btn4"></label>
        <span>Mr.</span>
    </div>
</div>

我认为遍历函数排序的逻辑是正确的,但我怀疑选择器值'input [value = 1]'的正确性。

类似的问题:jQuery value selector

1 个答案:

答案 0 :(得分:0)

试试这个:

 $('.a-choice1').click(function () {
          $(this).find('input[value=1]').addClass('highlight_selected_right');
 });

Demo: Inspect the last radio button and click-new

相关问题