JQUERY 检查 INPUT 文本值是否为空并提示警报

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

JQUERY check if INPUT text Value is empty and prompt an alert

javascriptjqueryformsvalidationsubmit

提问by The Masta

How is it possible to display an Alert with JQUERY if i click the submit button an the value of the imput field is empty?

如果我单击提交按钮并且输入字段的值为空,如何使用 JQUERY 显示警报?

<input type="text" id="myMessage" name="shoutbox_msg" size="16" class="field_nosize" maxlength="150">&nbsp;
<input id="submit" type="submit" name="submit_post" class="button_nosize" value="Senden" onclick="sendMessage(); clearInput();">

回答by Tushar Gupta - curioustushar

$('#submit').click(function(){
   if($('#myMessage').val() == ''){
      alert('Input can not be left blank');
   }
});

Update

更新

If you don't want whitespace also u can remove them using jQuery.trim()

如果你不想要空格,你也可以使用jQuery.trim()删除它们

Description: Remove the whitespace from the beginning and end of a string.

描述:删除字符串开头和结尾的空格。

$('#submit').click(function(){
   if($.trim($('#myMessage').val()) == ''){
      alert('Input can not be left blank');
   }
});

回答by Zigri2612

Better one is here.

更好的在这里。

$('#submit').click(function()
{
    if( !$('#myMessage').val() ) {
       alert('warning');
    }
});

And you don't necessarily need .length or see if its >0 since an empty string evaluates to false anyway but if you'd like to for readability purposes:

而且您不一定需要 .length 或查看它是否 >0 ,因为空字符串无论如何都会评估为 false 但如果您想出于可读性目的:

$('#submit').on('click',function()
{
    if( $('#myMessage').val().length === 0 ) {
        alert('warning');
    }
});

If you're sure it will always operate on a textfield element then you can just use this.value.

如果您确定它始终对文本字段元素进行操作,那么您可以使用 this.value。

$('#submit').click(function()
{
      if( !document.getElementById('myMessage').value ) {
          alert('warning');
      }
});

Also you should take note that $('input:text') grabs multiple elements, specify a context or use the this keyword if you just want a reference to a lone element ( provided theres one textfield in the context's descendants/children ).

此外,您应该注意 $('input:text') 抓取多个元素,指定上下文或使用 this 关键字,如果您只想引用一个单独的元素(前提是上下文的后代/子项中有一个文本字段)。

回答by Shiv Yadav

Also you can try this, if you want to focus on same text after error.

如果您想在错误后专注于相同的文本,您也可以尝试此操作。

If you wants to show this error message in a paragraph then you can use this one:

如果您想在段落中显示此错误消息,则可以使用以下内容:

 $(document).ready(function () {
    $("#submit").click(function () {
        if($('#selBooks').val() === '') {
            $("#Paragraph_id").text("Please select a book and then proceed.").show();
            $('#selBooks').focus();
            return false;
        }
    });
 });

回答by Super Model

Check empty input with removing space(if user enter space) from input using trim

使用trim从输入中删除空格(如果用户输入空格)检查空输入

$(document).ready(function(){     
       $('#button').click(function(){
            if($.trim($('#fname').val()) == '')
           {
               $('#fname').css("border-color", "red");
               alert("Empty"); 
           }
     });
});