Javascript 如何在jquery中检查表单是否已填写?

Javascript 如何在jquery中检查表单是否已填写?,javascript,jquery,html,forms,Javascript,Jquery,Html,Forms,我有一个表单、一个提交按钮和一个模式。如果表单有空值,我想运行模式来显示消息 我试过了。每次循环检查表单是否有空输入,但即使表单被填满,它仍然返回为空 我的html表单: <form id="register_form" role="form" > <label >First Name</label> <input id="inN"data-error="Enter first name." required> <

我有一个表单、一个提交按钮和一个模式。如果表单有空值,我想运行模式来显示消息

我试过了。每次循环检查表单是否有空输入,但即使表单被填满,它仍然返回为空

我的html表单:

  <form id="register_form" role="form" >
    <label >First Name</label>
    <input id="inN"data-error="Enter first name." required>
    <div class="help-block with-errors"></div>
    </div>
    <label>Last Name</label>
    <input id="inL"  data-error="Enter last name." required>
    <div class="help-block with-errors"></div>
    </div>
    </div>
          <label for="inputEmail" class="control-label">Email</label>
          <input type="email" class="form-control" id="inputEmail" data-error="Enter email." required>
          <div class="help-block with-errors"></div>
       </div>
    </div>
</form>
试试下面的代码

用以下代码替换jquery选择器

$('register\u form input')

你可以简单地修复它

试试下面的代码

用以下代码替换jquery选择器

$('register\u form input')

你可以简单地修复它

您必须使用
$(“#register_form input”)。每个
而不是
$(“#register_form”)。每个
都要循环遍历表单中的所有
输入
字段

请注意如果您在实际的
表单中有
选择框或
文本区域
字段
,则需要相应修改此代码。我还建议你看看插件,它可以为你完成所有这些工作

$(文档).ready(函数(){
$(“#提交按钮”)。单击(函数(){
ValidateForm();
});
});
函数ValidateForm(){
var formInvalid=假;
$(“#注册表_表单输入”)。每个(函数(){
if($(this.val()=''){
formInvalid=true;
}
});
if(formInvalid)
警报(“一个或两个字段为空,请填写所有字段”);
}

名字
姓
电子邮件
提交
您必须使用
$('register\u form input')。每个
而不是
$('register\u form')。每个
都可以循环使用表单中的所有
输入
字段

请注意如果您在实际的
表单中有
选择框或
文本区域
字段
,则需要相应修改此代码。我还建议你看看插件,它可以为你完成所有这些工作

$(文档).ready(函数(){
$(“#提交按钮”)。单击(函数(){
ValidateForm();
});
});
函数ValidateForm(){
var formInvalid=假;
$(“#注册表_表单输入”)。每个(函数(){
if($(this.val()=''){
formInvalid=true;
}
});
if(formInvalid)
警报(“一个或两个字段为空,请填写所有字段”);
}

名字
姓
电子邮件
提交

它总是在文本框中显示空白值,因为当JavaScript代码执行时,所有文本框都会重置,请查看下图以了解更多详细信息


它总是在文本框中显示空白值,因为当JavaScript代码执行时,所有文本框都会重置,请查看下图以了解更多详细信息


$(文档).ready(函数(){
$(“#注册表格”)
.formValidation({
框架:“bootstrap”,
错误:{
容器:“#错误”
},
图标:{
有效:“glyphicon glyphicon ok”,
无效:“glyphicon glyphicon删除”,
正在验证:“glyphicon glyphicon刷新”
},
字段:{
名字:{
行:'.col-xs-4',
验证器:{
注意:{
消息:“第一个名字是必需的”
}
}
},
姓氏:{
行:'.col-xs-4',
验证器:{
注意:{
消息:“姓氏是必需的”
}
}
},
用户名:{
验证器:{
注意:{
消息:“用户名是必需的”
},
弦长:{
民:6,,
最高:30,
消息:“用户名长度必须大于5个字符且小于31个字符”
},
regexp:{
regexp://^[a-zA-Z0-9\.]+$/,,
消息:“用户名只能由字母、数字、点和下划线组成”
}
}
},
电邮:{
验证器:{
注意:{
消息:“需要电子邮件地址”
},
电子邮件地址:{
消息:“电子邮件地址无效”
}
}
},
密码:{
验证器:{
注意:{
消息:“密码是必需的”
},
不同的:{
字段:“用户名”,
消息:“密码不能与用户名相同”
}
}
},
性别:{
验证器:{
注意:{
信息:“性别是必需的”
}
}
},
生物:{
验证器:{
弦长:{
最高:300,
信息:“简历长度必须少于300个字符”
}
}
}
}
})
.on('err.form.fv',函数(e){
//显示消息模式
$('#messageModal').modal('show');
});
});


$(文档).ready(函数(){
$(“#注册表格”)
.formValidation({
框架:“bootstrap”,
错误:{
容器:“#错误”
},
图标:{
有效:“glyphicon glyphicon ok”,
无效:“glyphicon glyphicon删除”,
正在验证:“glyphicon glyphicon刷新”
},
字段:{
名字:{
行:'.col-xs-4',
$('#register_form').each(function() {
            if ( $(this).val() === '' ){
                console.log($(this).val()); // empty even when form is not
                vm.message = 'One or Two fields are empty. Please fill up all fields'
                document.getElementById("message").textContent=vm.message; // message on the modal
            };


        });
 var allInput = $('input');

    $.each(allInput,function(){
     //looping through all input assuming u have one form in the page  u can pass form id and grab input too
    if($(this).val() == ""){
    alert('empty');
//do something append error msgs etc
    } else {
    alert('you can go');
    }

    });
<script>
$(document).ready(function() {
$('#signupForm')
    .formValidation({
        framework: 'bootstrap',
        err: {
            container: '#errors'
        },
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            firstName: {
                row: '.col-xs-4',
                validators: {
                    notEmpty: {
                        message: 'The first name is required'
                    }
                }
            },
            lastName: {
                row: '.col-xs-4',
                validators: {
                    notEmpty: {
                        message: 'The last name is required'
                    }
                }
            },
            username: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 5 and less than 31 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required'
                    },
                    emailAddress: {
                        message: 'The email address is not valid'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },
                    different: {
                        field: 'username',
                        message: 'The password cannot be the same as username'
                    }
                }
            },
            gender: {
                validators: {
                    notEmpty: {
                        message: 'The gender is required'
                    }
                }
            },
            bio: {
                validators: {
                    stringLength: {
                        max: 300,
                        message: 'The bio must be less than 300 characters long'
                    }
                }
            }
        }
    })
    .on('err.form.fv', function(e) {
        // Show the message modal
        $('#messageModal').modal('show');
    });
  });
 </script>