如何在 sweetalert 中设置字符限制?
How do I set a character limit in sweetalert?
$(document.getElementById("buttonClicked")).click(function(e) {
swal({
imageUrl: "http://www.petdrugsonline.co.uk/images/page-headers/cats-master-header",
title: "sentence: ",
text: "0/140",
type: "input",
showCancelButton: true,
closeOnConfirm: true,
closeOnCancel: true,
}, function(text) {
if (inputValue.length > 140) {
swal.showInputError("You need to write something!");
}
// fire post
$.post("/index.php", {
img: finalDataURL,
text: text
}).done(function(data) {
});
});
});
我想修改它,以便文本会针对输入中键入的每个字符进行更新(0/140 -> 1/140 -> 2/140 -> 等),我想这样做,如果用户尝试单击提交,但他们输入的字符数超过 140 个时,会弹出一个错误。
现在我的代码没有执行任何这些操作。
目前,SweetAlert 没有内置字符计数器,因此您必须稍微调整源代码才能获得您想要的。
对于这个错误,你是在正确的轨道上,只是遗漏了一些东西和一些逻辑:
...
function(text) {
// check the length of "text" instead of "inputValue"
// since that's what you're passing into the function
if (text.length > 140) {
swal.showInputError("You have exceeded 140 characters!");
return false;
} else {
// Only try submitting once character count validation has passed
$.post("/index.php", {
img: finalDataURL,
text: text
}).done(function(data) {
});
}
});
$(document.getElementById("buttonClicked")).click(function(e) {
swal({
imageUrl: "http://www.petdrugsonline.co.uk/images/page-headers/cats-master-header",
title: "sentence: ",
text: "0/140",
type: "input",
showCancelButton: true,
closeOnConfirm: true,
closeOnCancel: true,
}, function(text) {
if (inputValue.length > 140) {
swal.showInputError("You need to write something!");
}
// fire post
$.post("/index.php", {
img: finalDataURL,
text: text
}).done(function(data) {
});
});
});
我想修改它,以便文本会针对输入中键入的每个字符进行更新(0/140 -> 1/140 -> 2/140 -> 等),我想这样做,如果用户尝试单击提交,但他们输入的字符数超过 140 个时,会弹出一个错误。
现在我的代码没有执行任何这些操作。
目前,SweetAlert 没有内置字符计数器,因此您必须稍微调整源代码才能获得您想要的。
对于这个错误,你是在正确的轨道上,只是遗漏了一些东西和一些逻辑:
...
function(text) {
// check the length of "text" instead of "inputValue"
// since that's what you're passing into the function
if (text.length > 140) {
swal.showInputError("You have exceeded 140 characters!");
return false;
} else {
// Only try submitting once character count validation has passed
$.post("/index.php", {
img: finalDataURL,
text: text
}).done(function(data) {
});
}
});