我需要检查单选按钮输入字段是否使用Javascript/Jquery进行了检查。事实上,我正在动态设置单选按钮字段,但在检查该字段是否被选中时,它会在真正被选中的地方显示错误的结果。我的代码在下面。
<input type="radio" name="answer_type0" id="answer_type0" onClick="selectScale(this.value,'0');" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>
我的javascript代码如下。
$('#answer_type0[value="' + qdata[0].answer_type + '"]').prop('checked', true).trigger('click');
console.log('check',document.getElementById('answer_type0').checked);
在这里,我的无线电输入字段似乎在UI页面上被选中,但当我检查console.log
消息时,它显示为false。在这里,我需要如果单选按钮字段被选中,它应该会给出正确的消息,但它并不是这样发生的。
试着这样检查
if($('#answer_type0').is(':checked')){
console.log("true");
}else{
console.log("false");
}
您可以通过以下方法进行检查
if ($("#answer_type0").prop("checked")) {
alert("checked");
}
else
{
alert ("not checked");
}
// OR
if ($("#answer_type0").is(":checked")) {
alert("checked");
}
else
{
alert ("not checked");
}