检查输入值是否为空并显示警报

编程入门 行业动态 更新时间:2024-10-27 10:23:40
本文介绍了检查输入值是否为空并显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

How is it possible to display an alert with jQuery if I click the submit button and the value of the input 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'); } });

更多推荐

检查输入值是否为空并显示警报

本文发布于:2023-11-27 20:29:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   为空

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!