JQUERY检查INPUT文本值是否为空并提示警报 [英] JQUERY check if INPUT text Value is empty and prompt an alert

查看:83
本文介绍了JQUERY检查INPUT文本值是否为空并提示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果单击提交按钮,输入字段的值为空,如何显示带有JQUERY的警报?

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?

<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();">


推荐答案

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

更新

如果您不想要空白,也可以使用 jQuery.trim()将其删除。

If you don't want whitespace also u can remove them using 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');
   }
});

这篇关于JQUERY检查INPUT文本值是否为空并提示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆