2016-10-28 40 views
0

我有一個文本字段,我檢測到與jQuery的變化,然後我取消/激活一個按鈕,如果價值> 1000(它應該包含一個價格)。 它工作正常。輸入變化檢測不工作,而使用掩碼

現在我想對我的輸入應用掩碼格式值,並限制這樣的人物: 99 999 999,99

面膜是工作,但隨後,修改檢測不工作...

我使用autoNumeric爲我的面具: https://github.com/BobKnothe/autoNumeric

我創建了2個例子與的jsfiddle:

這裏是我的javascript代碼:

function activeInvoice(priceField) { 
     console.log(priceField.val()); 
     var val = (priceField.val() >= 1000); 
     $('#invoice_file').prop('disabled', !val); 
     $('#invoice_file').prop('required', val); 
    } 

    $(function() { 
     $('#price').autoNumeric("init", { 
      aSep: ' ', 
      aDec: ',' 
     }); 

     var priceField = $('#price'); 
     activeInvoice(priceField); 

     $(document).on('input', '#price', function() { 
      activeInvoice($(this)); 
     }); 
    }); 

我想有兩個工作,當然!

感謝

+1

有關信息,autoNumeric不發送'input'也不'change'事件是得到了[固定](錯誤HTTPS ://github.com/BobKnothe/autoNumeric/issues/292)。您現在可以透明地使用蒙版,而不會干擾默認事件(幾乎; p)。 – Alex

回答

1

嘗試使用此代碼:

 function activeInvoice(priceField) {    
 
      var priceField_value = priceField.val().replace(/ +/g, '');     
 
      var val = (parseInt(priceField_value) >= 1000); 
 
      $('#invoice_file').prop('disabled', !val); 
 
      $('#invoice_file').prop('required', val); 
 
     } 
 

 
     $(function() { 
 
      $('#price').autoNumeric("init", { 
 
       aSep: ' ', 
 
       aDec: ',' 
 
      }); 
 
      
 
      var priceField = $('#price'); 
 
      activeInvoice(priceField); 
 
      
 
      $(document).on('keyup', '#price', function() { activeInvoice($(this)); }); 
 
     });
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/autonumeric/1.9.46/autoNumeric.js"></script> 
 

 
<input id="price" type="text" /> 
 
<input id="invoice_file" type="button" value="test" />

+0

是的,它幾乎在那裏,謝謝,我想我需要做一些關於昏迷的事情,如果我的例子中有一個十進制值,它不起作用。 ('keyup','#price',function(){ activeInvoice($(this)); });我需要檢查keyup事件。 由於我的字段是動態加載的 – ThEBiShOp

+0

要處理逗號,請更新此代碼:var priceField_value = priceField.val()。replace(/ [,] +/g,''); – Soliyappan

+0

那麼如果我這樣做,10,00將被轉換爲1000這不是預期的:p 如果我使用你的第一個建議,然後做var val =(parseInt(priceField_value)> = 1000);它工作正常,你可以編輯,然後我會接受你的答案;)謝謝! – ThEBiShOp