无法使用JavaScript/jQuery验证单选按钮字段 [英] Can not validate the radio button field using JavaScript/jQuery
问题描述
我需要检查是否使用Javascript/Jquery选中了单选按钮输入字段.实际上,我是动态设置单选按钮字段,但是在检查是否选中此字段时,它向我显示在真正检查的地方结果为false.我的代码在下面.
I need to check if the radio button input field is checked or not using Javascript/Jquery. Actually I am setting the radio button field dynamically but while checking whether this field is checked or not it's showing me the result false where it is really checked. My code is below.
<input type="radio" name="answer_type0" id="answer_type0" onClick="selectScale(this.value,'0');" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>
我的JavaScript代码在下面.
My javascript code is below.
$('#answer_type0[value="' + qdata[0].answer_type + '"]').prop('checked', true).trigger('click');
console.log('check',document.getElementById('answer_type0').checked);
在这里,我的无线电输入字段在UI页面上似乎已选中,但是当我正在检查console.log
消息时,它显示为false.在这里,我需要如果选中单选按钮字段,它应该使消息正确,但不会那样.
Here my radio input field seems checked on the UI page but while i am checking the console.log
message its showing false. Here I need if the radio button field is checked it should give the message true but it's not happening like that.
推荐答案
尝试像这样检查.
Try checking like this.
if($('#answer_type0').is(':checked')){
console.log("true");
}else{
console.log("false");
}
这篇关于无法使用JavaScript/jQuery验证单选按钮字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!