用于jQuery查找方法中的标记

时间:2017-04-18 10:28:14

标签: jquery

我必须检查属性是否设置为label,并将其映射到值为radio的{​​{1}}按钮。

HTML

abc

现在,我的值为<input type="radio" id="radio1" name="radio" value="abc" class="buttonset ui-helper"> <label for="radio1" class="ui-button ui-state-active" role="button" aria-pressed="true"><span class="ui-button-text">tesData</span></label> ,现在我必须找到映射的abc是否具有属性label(for="radio1")

我试过了,

aria-pressed="true"

我在哪里可以检查类值是否为if ($("input [value=abc]").find('label [for=radio1]').hasClass('aria-pressed')) { //if true do something } 以及如何为输入和标签映射true标记。

3 个答案:

答案 0 :(得分:2)

我认为你需要以下

self.scrollView.keyboardDismissMode = .interactive
// use attribute selectors for the label and include the id of the input as the for in the label
// the .length says if there are any present, then do the stuff in your if
if($('label[aria-pressed=true][for=' + $("input[value=abc]").attr('id') + ']').length) {
    console.log('hi');
}

答案 1 :(得分:1)

aira-pressed不是一个类,而是一个attribute。因此,不要使用hasClass('aria-pressed')尝试使用attr('aria-pressed')

if ($("input[value=abc]").next('label[for=radio1]').attr('aria-pressed') == "true")
{
     console.log("works")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" id="radio1" name="radio" value="abc" class="buttonset ui-helper">
<label for="radio1" class="ui-button ui-state-active" role="button" aria-pressed="true"><span class="ui-button-text">tesData</span></label>

更动态的解决方案:

aira-pressed不是一个类,而是一个attribute。因此,不要使用hasClass('aria-pressed')尝试使用attr('aria-pressed')

$("input[value=abc]").each(function() {
  var lab = $(this).next('label[for="' + $(this).attr("id") + '"');
  if (lab.attr('aria-pressed') == "true") {
    console.log("Label for: " + lab.attr("for") + ", has aria-pressed = true" )
  }
  else {
    console.log("Label for: " + lab.attr("for") + ", has aria-pressed = false" )
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" id="radio1" name="radio" value="abc" class="buttonset ui-helper">
<label for="radio1" class="ui-button ui-state-active" role="button" aria-pressed="true"><span class="ui-button-text">tesData</span></label>

<input type="radio" id="radio2" name="radio" value="abc" class="buttonset ui-helper">
<label for="radio2" class="ui-button ui-state-active" role="button" aria-pressed="false"><span class="ui-button-text">tesData2</span></label>

答案 2 :(得分:0)

最好的动态方式是。

var input_val="abc";
if ($('label[for="'+$('input[value="'+input_val+'"]').attr('id')+'"]').attr('aria-pressed') == "true")
{
     console.log("it works")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" id="radio1" name="radio" value="abc" class="buttonset ui-helper">
<label for="radio1" class="ui-button ui-state-active" role="button" aria-pressed="true"><span class="ui-button-text">tesData</span></label>