我的代码中使用了 4 个无线电组。其中 2 个是必需的无线电组。在提交表格时,我需要检查是否检查了所需的无线电组。如果未选中,我需要在下一个 div 中添加一条错误消息。但这对我不起作用。一些身体可以帮助我吗?下面是一个广播组。所以我试图遍历单选按钮并检查它是否未选中,添加错误消息。但这似乎不起作用。两个单选按钮都会显示“请选择一个”文本。我只需要在第二个单选按钮下方的一个地方使用它。
$("input:radio").each(function(){
$(this).next('div.error').remove();
var name = $(this).attr("name");
if($("input:radio[name="+name+"]:checked").length == 0){
$(this).after('<div class="error">Please select one</div>');
returnValue = false;
}
else{
$(this).next('div.error').remove();
returnValue = true;
}
});
HTML
<input name="radio1" id="one" value="first" type="radio" class = "check"/>
<input name="radio1" id="two" value="second" type="radio" class="check"/>