本文介绍了检查输入值是否为空并显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我单击提交"按钮并且输入字段的值为空,如何使用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">
<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()
$('#submit').click(function(){
if($.trim($('#myMessage').val()) == ''){
alert('Input can not be left blank');
}
});
这篇关于检查输入值是否为空并显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!