如何在sweetalert中设置字符限制

How do I set a character limit in sweetalert?

本文关键字:字符 设置 sweetalert      更新时间:2023-09-26
    $(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) {
        });
    }
 });