我有一个令人烦恼的长单选按钮问题/答案列表
Do you like A or B
Do you prefer x or y
这些类型的问题。
标记看起来有点像:
<input type="radio" name="User[AnnoyingQuestion][1]" value="1|Do you like x or y?|X" id="Member[PartyProfile]_1_0" />
<input type="radio" name="User[AnnoyingQuestion][1]" value="1|Do you like x or y?|Y" id="Member[PartyProfile]_1_1" class="elp-radio-question">
基本上,我需要确保所有问题都得到解答。 使用 jQuery 执行此操作的好策略是什么?
最佳答案
证明:http://jsfiddle.net/iambriansreed/vXXUV/
将表单选择器设置得更精确。
<script>
$(function(){
$('form').submit(function(e){
var radios = {}, success = true;
$('input[type="radio"]').each(function(){
var name = $(this).attr('name');
if(name in radios) return true;
radios[name] = $('input[name="'+name+'"]:checked').length > 0;
});
for(name in radios){
if(!radios[name]){
success = false;
alert('Please enter a value for '+name);
}
}
if(!success) e.preventDefault();
else alert('Form Submitting...');
});
});
</script>
关于jquery - 使用 jQuery 确保所有单选按钮都有一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10197973/