如何在检查之前禁用表单提交?

如果未选择无线电,我有一个表单并尝试禁用发布:

function checkForm(obj){ var return_value = true; var error_msg = 'Some text: '+'\n'; $("input[type='radio']:not(:checked)").each(function() { error_msg += $(this).next().text() + ", "; }); if(!return_value) alert(error_msg); return return_value; } 

形成:

 

My question

  • a1
  • a2
  • a3
  • a4

但没有警报显示,表单已提交。

你可以尝试一下

 $('form[name=test1]').submit(function(){ var return_value = false; var error_msg = 'Some text: '+'\n'; $("input[type='radio']").each(function() { if ($(this).attr('checked')){ return_value = true; } else { error_msg += $(this).next().text() + ", "; } }); if(!return_value) alert(error_msg); return return_value; } ); 

并在html中删除onsubmit函数

如果未选中单选按钮,则应将return_value设置为false ,因为初始值为true