【发布时间】:2015-08-25 05:43:33
【问题描述】:
我正在制作一个带有单选按钮的表单。我能够验证除单选按钮之外的所有其他输入。我在这个网站上得到了一个公认的解决方案
Using JQuery to check if no radio button in a group has been checked
但这对我不起作用
http://jsfiddle.net/ghan_123/trr185L2/2/
我的代码
<input type='radio' name='datetime' value='1'>1
<input type='radio' name='datetime' value='2'>2
<input type='radio' name='datetime' value='3'>3
<input type='radio' name='datetime' value='4'>4
<input type='radio' name='datetime' value='5'>5
<br><button id='buttonnew'>validate</button>
jquery
$(document).ready(function(){
$("#buttonnew").click(function(){
var selection=document.querySelector('input[name="datetime"]:checked').value;
// my method
if(selection=='')
{
alert('please select a value');
}
else
{
alert('your selected value is '+selection);
}
// end my method
// other method ( given in this site link above)
if (!$("input[name='datetime']:checked").val()) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
// end other method
});
});
如果未检查任何内容,则不会出现警报消息。 否则,两者都会在选择其中之一时发出警报消息。
【问题讨论】:
-
查看link