javascript - 单击 swal 的确定按钮后退格键不起作用
问题描述
我在下面写了几行代码
$(".only-numbers").keypress(function(e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
swal({
title: "Warning",
type: "warning",
text: "Please enter Numbers only"
});
return false;
} else {
return true;
}
});
$("#driver_contact_number").on("keyup keydown change", function(event) {
var max_chars = 12;
if ($(this).val().length >= max_chars) {
swal({
title: "Warning",
type: "warning",
text: "Number should not exceed 12 digits"
});
$(this).val($(this).val().substr(0, max_chars));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
<input id="driver_contact_number" class="only-numbers">
每当用户输入大于 12 位时,就会显示 swal 警告消息,但是在单击 swal 弹出窗口的 ok 按钮后,当我们单击退格时,它不起作用。请帮忙!!!
解决方案
解除警报后,您可以利用承诺重新关注您的来源。
其他变化:
- 我不知道为什么你有 else ,但我在不需要时删除了它。
- 你有一个弃用的
type
所以我把它改成icon
- 您的长度函数有 >= 但应该是 > 导致退格问题
$(".only-numbers").on('keypress', function(e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
swal({
title: "Warning",
icon: "warning",
text: "Please enter Numbers only"
}).then(function() {
$(e.target).focus();
});
return false;
}
return true;
});
$("#driver_contact_number").on("keyup keydown change", function(event) {
var max_chars = 12;
if ($(this).val().length > max_chars) {
swal({
title: "Warning",
icon: "warning",
text: "Number should not exceed 12 digits"
}).then(function() {
$(event.target).focus();
});
$(this).val($(this).val().substr(0, max_chars));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
<input id="driver_contact_number" class="only-numbers">
推荐阅读
- html - 将 svg 图像转换为按钮
- mysql - 创建引用复合主键父级的外键的问题
- c++ - 来自 main() 函数的意外返回值
- linux - caps2esc 停止使用 systemd 问题
- android - 当应用程序处于后台或被杀死时,FCM onMessageReceived() 未被调用
- .net - 连接字符串中缺少必需的空白错误是什么?
- java - 我使用递归按字母顺序获取第一个字符串的 java 代码给出了不正确的输出。我需要添加辅助方法吗?
- mysql - Discord.js 中的节点 mysql 返回空结果/数组
- sql - 具有多个(相同ID不同日期和不同图像)的SQL服务器查询(按最后日期过滤)
- kotlin - XMLPullParser 只返回一个元素