【发布时间】:2018-05-12 17:50:31
【问题描述】:
当表单所需的文本框为空并且用户单击提交按钮时,如何弹出一个甜蜜的警报错误。我想在甜蜜警报中显示 %This TextBox Is Required% 的即时弹出危险警报。比如文本框为空时的 JavaScript 警报,不同的是,这是 js 警报,我说的是甜警报而不是简单的 js 警报。请帮忙...
【问题讨论】:
标签: javascript jquery html css
当表单所需的文本框为空并且用户单击提交按钮时,如何弹出一个甜蜜的警报错误。我想在甜蜜警报中显示 %This TextBox Is Required% 的即时弹出危险警报。比如文本框为空时的 JavaScript 警报,不同的是,这是 js 警报,我说的是甜警报而不是简单的 js 警报。请帮忙...
【问题讨论】:
标签: javascript jquery html css
如果输入文本为空,这里会发出温馨提示
$('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>
【讨论】:
你需要在这里检查你使用 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>
【讨论】:
.swal-footer { text-align:center !important; } 添加到 CSS 中