javascript 检查 jQuery 中的 textarea 是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24096788/
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-10-28 02:10:54  来源:igfitidea点击:

Checking if textarea is empty in jQuery

javascriptjquerytextarea

提问by Sebastian

Here's my code:

这是我的代码:

if ($('#inputName').val() && $('#inputEmail').val() && $('#inputInstrument').val() && $('#inputFee').val() $('#message').val() != '')
    {
        alert('success');
    }
    else
    {
        alert('fill in all fields');
    }

If I take out the last condition (#message), it works fine, but with it in I get:

如果#message我去掉最后一个条件 ( ),它可以正常工作,但是有了它,我会得到:

Uncaught SyntaxError: Unexpected identifier

未捕获的语法错误:意外的标识符

Here's the HTML:

这是 HTML:

<form class="form-horizontal" role="form" action="includes/data.php" method="POST" id="findmusicians">
    <div class="form-group">
        <label for="inputName" class="col-sm-3 control-label">* Name</label>
    <div class="col-md-7">
            <input type="text" name="inputName" class="form-control required" minlength="1" id="inputName">
            <div id="hidden"></div>
    </div>
    </div>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-3 control-label">* Email</label>
    <div class="col-md-7">
            <input type="email" name="inputEmail" class="form-control" id="inputEmail">
    </div>
    </div>
<div class="form-group">
    <label for="inputPhone" class="col-sm-3 control-label">Telephone</label>
    <div class="col-md-7">
            <input type="text" name="inputTelephone" class="form-control" id="inputTelephone">
    </div>
</div>
<div class="form-group">
    <label for="inputInstrument" class="col-sm-3 control-label">* Instrument(s)</label>
    <div class="col-md-7">
        <input type="text" name="inputInstrument" class="form-control" id="inputInstrument">
    </div>
</div>
<div class="form-group">
        <label for="inputFee" class="col-sm-3 control-label">* Fee</label>
        <div class="col-md-7">
            <input type="text" name="inputFee" class="form-control" id="inputFee">
        </div>
    </div>
<div class="form-group">
    <label for="message" class="col-sm-3 control-label">* Message</label>
    <div class="col-md-9">
        <textarea name="message" id="message" class="form-control" rows="3" style="height: 200px"></textarea>
        <span class="help-block" style="text-align: left;">Please include as much information as possible including reperttheitroade, rehearsal/performance times and venues.</span>
    </div>
</div>
<div class="form-group">
    <div class="col-md-3 col-md-offset-3">
        <button id="findmusicians-submit" type="submit" class="btn btn-success">Submit request</button>
    </div>
</div>
<div class="col-md-9 col-md-offset-3" style="text-align: left; padding-left: 5px">
    <span id="result" class="text-success"></span>
</div>
</form>

回答by source.rar

Just adding my comment as an answer.

只需添加我的评论作为答案。

The problem is that you've missed out the &&before the last condition check.

问题是您错过了&&上次条件检查之前的内容。

if ($('#inputName').val() && $('#inputEmail').val() && $('#inputInstrument').val() && $('#inputFee').val() && $('#message').val() != '')

Although that works, you may want to trim() and check the textarea content length.

虽然这可行,但您可能需要修剪()并检查 textarea 内容长度。

($('#message').val().trim().length > 0)

回答by razahassank

You forgot && before the last condition as source.rar mentioned.

您在 source.rar 提到的最后一个条件之前忘记了 &&。

Also this isn't the correct way to check that textarea is empty. $('#message').val() != ''

这也不是检查 textarea 是否为空的正确方法。$('#message').val() != ''

Might be someone type spaces.. so first trim the value and then check..

可能有人输入了空格..所以首先修剪值然后检查..

if ($.trim($('#message').val()).length > 0)
{

}

回答by RGS

You have missed &&before last condition.

您在最后一个条件之前错过了&&

 $('#findmusicians-submit').click(function(e){
    if ($('#inputName').val()!= '' && $('#inputEmail').val()!= '' && $('#inputInstrument').val()!= '' && $('#inputFee').val()!= '' && $('#message').val() != '')
        {
            alert('success');
        }
        else
        {
            alert('fill in all fields');
        }
 });

Demo:

演示:

http://jsfiddle.net/9W4Wa/

http://jsfiddle.net/9W4Wa/

回答by Jai

so instead you can do this:

所以你可以这样做:

var inputVals = $('#inputName, #inputEmail, #inputInstrument, #inputFee, #message')
if(inputVals.val() != '')

or:

或者:

var inputVals = $('input[id*="input"], #message')
if(inputVals.val() != '')