如何在sweetalert中设置字符限制?

时间:2016-07-19 20:58:55

标签: javascript jquery 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,则会弹出错误。

现在我的代码没有做任何事情。

1 个答案:

答案 0 :(得分:1)

目前,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) {

        });
    }
 });