我需要一个帮助.我需要通过获取id来检查单选按钮,并使用
Javascript / Jquery进行验证.我在下面解释我的代码
<div> <input type="radio" name="rd0" value="57db18">Raj <input type="radio" name="rd0" value="57da17">Rahul <input type="radio" name="rd0" value="57db19">Mona </div> <br> <br> <div> <input type="radio" name="rd1" value="57db18">A <input type="radio" name="rd1" value="57da17">B <input type="radio" name="rd1" value="57db19">C </div> <br> <br> <div > <input type="radio" name="rd2" value="57db18">Apple <input type="radio" name="rd2" value="57da17">Orange <input type="radio" name="rd2" value="57db19">Mango </div> <span> <button type="button" id="btn" name="btn" onclick="setValue()">Set</button> </span> <span> <button type="button" id="vbtn" name="vbtn" onclick="validateRadioButtonValue()">Validate</button> </span>
在这里我有3套单选按钮,当用户点击设置按钮时,单选按钮应该按循环中的给定值进行检查.脚本部分如下.
var valu = ['57da17','57db18','57db19']; function setValue(){ for(var i=0;i<valu.length;i++){ $('#rd'+i+'[value="' + valu[i]+ '"]').prop('checked',true); console.log('checked btn',$('#rd'+i).is(':checked') ); } }
但是在目前的情况下,它没有发生.当用户也将点击验证按钮时,应该检查所有单选按钮是否被检查.请帮助我.
解决方法
尝试这个 :
$(‘input [name = rd’i’] [value =“’valu [i]’”]’).prop(‘checked’,true)
var valu = ['57da17','57db19']; function setValue(){ for(var i=0;i<valu.length;i++){ $('input[name=rd'+i+'][value="' + valu[i]+ '"]').prop('checked',$('input[name=rd' + i + ']').is(':checked') ); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="radio" name="rd0" value="57db18">Raj <input type="radio" name="rd0" value="57da17">Rahul <input type="radio" name="rd0" value="57db19">Mona </div> <br> <br> <div> <input type="radio" name="rd1" value="57db18">A <input type="radio" name="rd1" value="57da17">B <input type="radio" name="rd1" value="57db19">C </div> <br> <br> <div > <input type="radio" name="rd2" value="57db18">Apple <input type="radio" name="rd2" value="57da17">Orange <input type="radio" name="rd2" value="57db19">Mango </div> <span> <button type="button" id="btn" name="btn" onclick="setValue()">Set</button> </span> <span> <button type="button" id="vbtn" name="vbtn" onclick="validateRadioButtonValue()">Validate</button> </span>