27

在不加载的情况下提交时如何使用 jQuery 检查文本字段是否为空login.php

<form action="login.php" method="post">
    <label>Login Name:</label>
    <input type="text" name="email" id="log" />
    <label>Password:</label>
    <input type="password" name="password" id="pwd" />
    <input type="submit" name="submit" value="Login" />
</form>

谢谢。

4

8 回答 8

46

You can bind an event handler to the "submit" JavaScript event using jQuery .submit method and then get trimmed #log text field value and check if empty of not like:

$('form').submit(function () {

    // Get the Login Name value and trim it
    var name = $.trim($('#log').val());

    // Check if empty of not
    if (name  === '') {
        alert('Text-field is empty.');
        return false;
    }
});

FIDDLE DEMO

于 2013-05-15T04:40:24.610 回答
11

您可以使用“必需” http://jsbin.com/atefuq/1/edit

<form action="login.php" method="post">
    <label>Login Name:</label>
    <input required type="text" name="email" id="log" />
    <label>Password:</label>
    <input required type="password" name="password" id="pwd" />
    <input  required type="submit" name="submit" value="Login" />
</form>
于 2013-05-15T04:46:00.930 回答
11

一个简单的解决方案是这样的

$( "form" ).on( "submit", function() { 

   var has_empty = false;

   $(this).find( 'input[type!="hidden"]' ).each(function () {

      if ( ! $(this).val() ) { has_empty = true; return false; }
   });

   if ( has_empty ) { return false; }
});

注意:该jQuery.on()方法仅在 jQuery 1.7+ 版本中可用,但它现在是附加事件处理程序的首选方法。

此代码循环遍历表单中的所有输入,并通过在其中任何一个没有值时返回 false 来阻止表单提交。请注意,它不会向用户显示任何关于表单提交失败的原因的消息(我强烈建议添加一个)。

或者,您可以查看jQuery 验证插件。它可以做到这一点以及更多。

注意:这种技术应始终与服务器端验证结合使用。

于 2013-05-15T04:48:00.730 回答
3

您需要向表单提交事件添加处理程序。在处理程序中,您需要检查每个文本字段,如果值不为空,请选择元素和密码字段。

$('form').submit(function() {
     var res = true;
     // here I am checking for textFields, password fields, and any 
     // drop down you may have in the form
     $("input[type='text'],select,input[type='password']",this).each(function() {
         if($(this).val().trim() == "") {
             res = false; 
         }
     })
     return res; // returning false will prevent the form from submitting.
});
于 2013-05-15T04:49:34.220 回答
3

I really hate forms which don't tell me what input(s) is/are missing. So I improve the Dominic's answer - thanks for this.

In the css file set the "borderR" class to border has red color.

$('#<form_id>').submit(function () {
    var allIsOk = true;

    // Check if empty of not
    $(this).find( 'input[type!="hidden"]' ).each(function () {
        if ( ! $(this).val() ) { 
            $(this).addClass('borderR').focus();
            allIsOk = false;
        }
    });

    return allIsOk
});
于 2015-05-24T10:48:40.123 回答
1

您应该尝试使用 jquery validate 插件:

$('form').validate({
   rules:{
       email:{
          required:true,
          email:true
       }
   },
   messages:{
       email:{
          required:"Email is required",
          email:"Please type a valid email"
        }
   }
})
于 2013-05-15T04:46:46.380 回答
1
function isEmpty(val) {
if(val.length ==0 || val.length ==null){
    return 'emptyForm';
}else{
    return 'not emptyForm';
}

}

$(document).ready(function(){enter code here
    $( "form" ).submit(function( event ) {
        $('input').each(function(){
           var getInputVal = $(this).val();
            if(isEmpty(getInputVal) =='emptyForm'){
                alert(isEmpty(getInputVal));
            }else{
                alert(isEmpty(getInputVal));
            }
        });
        event.preventDefault();
    });
});
于 2016-10-28T09:15:35.890 回答
0
var save_val = $("form").serializeArray();
$(save_val).each(function( index, element ) {
    alert(element.name);
    alert(element.val);
});
于 2017-07-26T08:27:22.197 回答