2016-07-19 72 views
0
$(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中設置字符限制?

現在我的代碼沒有這樣做。

回答

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) { 

     }); 
    } 
});