jQuery 如何检查输入字段是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15268236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:41:30  来源:igfitidea点击:

how to check if input field is empty

jquery

提问by Kakitori

I'm making a form with inputs, if the input type is empty then the button submit is disabled but, if the input fields is with length > 0 the submit button is enabled

我正在制作一个带有输入的表单,如果输入类型为空,则提交按钮被禁用,但是,如果输入字段的长度 > 0,则提交按钮被启用

<input type='text' id='spa' onkeyup='check()'><br>
<input type='text' id='eng' onkeyup='check()'><br>
<input type='button' id='submit' disabled>

function check() {
  if($("#spa").lenght>0 && $("#eng").lenght>0) {
    $("#submit").prop('disabled', false);
  } else {
    $("#submit").prop('disabled', true);
  }
}

it works but if then I delete for example the content of input spa the button submit is still enabled

它有效,但如果我删除例如输入 spa 的内容,按钮提交仍处于启用状态

回答by Ravi Gadag

Use trim and val.

使用修剪和 val。

var value=$.trim($("#spa").val());

if(value.length>0)
{
 //do some stuffs. 
}

val(): return the value of the input.

val(): 返回输入的值。

trim(): will trim the white spaces.

trim(): 将修剪空白。

回答by gdoron is supporting Monica

use .val(), it will return the value of the <input>

使用.val(),它将返回<input>

$("#spa").val().length > 0

And you had a typo, lengthnot lenght.

你有一个错字,length不是lenght

回答by Pete Uh

As javascript is dynamically typed, rather than using the .length property as above you can simply treat the input value as a boolean:

由于 javascript 是动态输入的,而不是像上面那样使用 .length 属性,您可以简单地将输入值视为布尔值:

var input = $.trim($("#spa").val());

if (input) {
    // Do Stuff
}

You can also extract the logic out into functions, then by assigning a class and using the each()method the code is more dynamic if, for example, in the future you wanted to add another input you wouldn't need to change any code.

您还可以将逻辑提取到函数中,然后通过分配一个类并使用该each()方法,代码会更加动态,例如,如果将来您想添加另一个输入,而无需更改任何代码。

So rather than hard coding the function call into the input markup, you can give the inputs a class, in this example it's test, and use:

因此,与其将函数调用硬编码到输入标记中,不如给输入一个类,在本例中为test,并使用:

$(".test").each(function () {
    $(this).keyup(function () {
        $("#submit").prop("disabled", CheckInputs());
    });
});

which would then call the following and return a boolean value to assign to the disabledproperty:

然后将调用以下内容并返回一个布尔值以分配给该disabled属性:

function CheckInputs() {
    var valid = false;
    $(".test").each(function () {
        if (valid) { return valid; }
        valid = !$.trim($(this).val());
    });
    return valid;
}

You can see a working example of everything I've mentioned in this JSFiddle.

您可以看到我在此 JSFiddle 中提到的所有内容的工作示例

回答by RobinV91nl

Why don't u use:

你为什么不使用:

<script>
$('input').keyup(function(){
if(($('#eng').val().length > 0) && ($('#spa').val().length > 0))
    $("#submit").prop('disabled', false);
else
    $("#submit").prop('disabled', true);
});
</script>

Then delete the onkeyup function on the input.

然后删除输入上的 onkeyup 函数。

回答by K D

Try this

尝试这个

$.trim($("#spa").val()).length > 0

It will not treat any white space if any as a correct value

它不会将任何空格(如果有)视为正确值

回答by YogeshWaran

if you are using jquery-validate.jsin your application then use below expression.

如果您jquery-validate.js在应用程序中使用,则使用以下表达式。

if($("#spa").is(":blank"))
{
  //code
}

回答by Kevin Bowersox

This snippet will handle more than two checkboxes in case you decide to expand the form.

如果您决定扩展表单,此代码段将处理两个以上的复选框。

$("input[type=text]").keyup(function(){
    var count = 0, attr = "disabled", $sub = $("#submit"), $inputs = $("input[type=text]");  
    $inputs.each(function(){
        count += ($.trim($(this).val())) ? 1:0;
    });
    (count >= $inputs.length ) ? $sub.removeAttr(attr):$sub.attr(attr,attr);       
});

Working Example:http://jsfiddle.net/sr4gq/

工作示例:http : //jsfiddle.net/sr4gq/