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

Jquery 如何在检查表单提交之前禁用表单提交?,jquery,submit,Jquery,Submit,我有一个表单,如果未选择收音机,则尝试禁用post: function checkForm(obj){ var return_value = true; var error_msg = 'Some text: '+'\n'; $("input[type='radio']:not(:checked)").each(function() { error_msg += $(this).next()

我有一个表单,如果未选择收音机,则尝试禁用post:

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;
        }
表格:

但是没有显示警报并且表单已提交。

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

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

您可以尝试此操作

$('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的remove onsubmit函数中,您可以尝试一下

$('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中,remove onsubmit函数可以完美地工作,但不能显示id。我使用的Jus null:error_msg+=$this.next.attr'id'+;您的任何具有值和名称属性的广播框都没有id。要获得价值,请使用$this.next.valWork完美,但不要显示id。我使用的Jus null:error_msg+=$this.next.attr'id'+;您的任何具有值和名称属性的广播框都没有id。要获取值,请使用$this.next.val