您可以从禁用提交按钮开始:
<input id="1" type="radio" name="1" value="1" />
<input id="2" type="radio" name="2" value="2" />
<input type="submit" disabled name="submit" value="valj" />
然后使用脚本绑定到单选按钮的更改事件并检查它们的值并在需要时删除 disabled 属性,类似于:
// cache reference to all radio buttons.
var $radioButtons = $("input:radio");
$radioButtons.change(function(){
var anyRadioButtonHasValue = false;
// iterate through all radio buttons
$radioButtons.each(function(){
if(this.checked){
// indicate we found a radio button which has a value
anyRadioButtonHasValue = true;
// break out of each loop
return false;
}
});
// check if we found any radio button which has a value
if(anyRadioButtonHasValue){
// enable submit button.
$("input[name='submit']").removeAttr("disabled");
}
else{
// else is kind of redundant unless you somehow can clear the radio button value
$("input[name='submit']").attr("disabled", "");
}
});
演示- 如果选择了任何单选按钮,则启用按钮。