2017-11-18 95 views
0

我正在使用sweetalert2 v7.0.0,我試圖在數量字段爲空時顯示錯誤。如何在sweetalert2中顯示ValidationError

當我在第一次嘗試時輸入金額時,它的工作原理沒有問題。

當我第一次嘗試離開字段爲空時,我得到一個驗證錯誤。之後,當我寫入金額並單擊提交按鈕驗證錯誤不會消失。它卡住不會再次驗證。這是jsfiddle link。如果你能告訴我方式,我將不勝感激。我在哪裏做錯了?

<a href="#" class="refundbutton"><i class="fa fa-undo"></i> Refund</a> 

$(".refundbutton").click(function(){ 
    swal({ 
     title: 'Enter Refund Details', 
     html:'<label for="ipt" class=" control-label"><span class="oval">1</span> Select refund type</label><select class="form-control refund_type"><option value="4"> Credit Card </option> <option value="5"> Cash </option></select>' + 
     '<label for="ipt" class=" control-label"><span class="oval">2</span> Enter refund amount</label><input type="text" class="form-control nDiscount_fixed" autocomplete="off" style="height: 34px;" value="">', 
     showCancelButton: true, 
     confirmButtonText: 'Submit', 
      preConfirm: function (value) { 
     return new Promise(function (resolve, reject) { 

      if ($(".nDiscount_fixed").val() === "") { 
       swal.showValidationError(
       'please enter a refund amount.' 
      ) 
      } 
      resolve([ $('.refund_type').val(), $('.nDiscount_fixed').val() ]) 

     }) 
     } 
    }).then(function (result) { 
    var swalrebateamount = parseFloat($('.nDiscount_fixed').val()).toFixed(2); 

    if (swalrebateamount !="" && swalrebateamount !="0") { 
    //some code 
    } 
    }).catch(swal.noop) 
    }); 

回答

3

您必須重置驗證錯誤。 more details

if ($(".nDiscount_fixed").val() === "") { 
       swal.showValidationError(
       'please enter a refund amount.' 
      ) 
    } 
    else 
    { 
      swal.resetValidationError(); 
    } 
+1

我錯過了這個方法。非常感謝你。我不知道該怎麼感謝你才足夠。 – hijacker83

+0

謝謝,你救了我的一天 – ManojMeria