当表单所需的文本框为空且用户单击“提交”按钮时,如何生成甜蜜警报错误弹出窗口。我想在甜蜜的警报中显示%此TextBox需要%的即时弹出危险警报。像文本框空不同的JavaScript提醒就是,这是js警报,而我正在谈论甜蜜警报,而不是简单的js警报。请帮忙......
答案 0 :(得分:1)
如果输入文字为空,这里是甜蜜警报
$('input[name="submit"]').on('click', function() {
if ($('input[name="fullname"]').val() == "") {
swal("fullname is required", '', 'error');
return false;
}
return true;
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<form action="#" method="POST">
<input type="text" name="fullname">
<input type="submit" value="submit" name="submit">
</form>
&#13;
答案 1 :(得分:0)
你需要在这里检查你使用jquery trim
和val
函数。
val :获取输入的当前值
trim :从字符串的开头和结尾删除空格。
$('#button').on('click', function(){
if($.trim($('input[type="text"]').val()) == ''){
swal("Input can not be left blank")
return false;
}
return true;
})
.swal-footer {
text-align:center !important;
}
.swal-button {
background-color: red;
}
.swal-button:focus {
outline: none;
box-shadow: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<form action="#" method="POST">
<input type="text" name="fullname">
<input type="text" name="lastname">
<input type="submit" value="submit" id="button" name="submit"> </form>