jQuery查找输入检查无线电的位置

时间:2016-09-07 08:58:46

标签: javascript jquery



    $().ready(function () {
        $(".div").find("input").each(function (index) {
            if($(this).attr("checked")==true){
                console.log("You have checked the"+index);
            }
        });
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
    <input name="1" type="radio" >
    <input name="1" type="radio"  checked="checked" >
    <input name="1" type="radio" >
    <input name="1" type="radio" >
</div>
&#13;
&#13;
&#13;

为什么我不能使用&#34;找到&#34;和&#34;每个&#34;功能,以证明检查无线电的哪些方面?

5 个答案:

答案 0 :(得分:1)

属性值为'checked',而不是true

if ($(this).attr("checked") == 'checked') {
  console.log("You have checked the " + index);
}

另请注意,您可以使用:checked选择器将索引检索设为一行:

&#13;
&#13;
$(document).ready(function() {
  var index = $(".div").find("input:checked").index();

  console.log("You have checked the " + index);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  <input name="1" type="radio">
  <input name="1" type="radio" checked="checked">
  <input name="1" type="radio">
  <input name="1" type="radio">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

console.log("You have checked the " + $("input[name=1]:checked").index());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  <input name="1" type="radio">
  <input name="1" type="radio" checked="checked">
  <input name="1" type="radio">
  <input name="1" type="radio">
</div>

无需使用.each()使用选择器:checked

  

描述:匹配检查或选中的所有元素。

答案 2 :(得分:0)

问题在于,只要您执行$(this).attr("checked")它就不会返回true,而是会为已检查的广播返回checkedundefined那些没有检查过的。

&#13;
&#13;
$().ready(function () {
        $("div").find("input").each(function (index) {
          if($(this).attr("checked")=="checked"){
                console.log("You have checked the "+index);
            }
        });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
    <input name="1" type="radio" >
    <input name="1" type="radio"  checked="checked" >
    <input name="1" type="radio" >
    <input name="1" type="radio" >
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

你可以找到以下小提琴。

&#13;
&#13;
$('#myForm input').on('change', function() {
   alert($('input[name=radioName]:checked', '#myForm').val()); 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" value="1" /> 1 <br />
<input type="radio" name="radioName" value="2" /> 2 <br />
<input type="radio" name="radioName" value="3" /> 3 <br />
</form>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您可以使用以下代码查找所选单选按钮的值:

<强> HTML

<table>
    <tr>
                    <td><input type="radio" name="q12_3" value="1">1</td>
                    <td><input type="radio" name="q12_3" value="2">2</td>
                    <td><input type="radio" name="q12_3" value="3">3</td>
                    <td><input type="radio" name="q12_3" value="4">4</td>
                    <td><input type="radio" name="q12_3" value="5">5</td>
                </tr>
</table>

<强> JQUERY

$(function(){
    $("input[type=radio]").click(function(){        
        alert($('input[name=q12_3]:checked').val());
    });
});

FIDDLE

相关问题