2015-04-20 49 views
0

我有這樣的代碼:阻止按鈕後所需值輸入

<script> 
jQuery(document).ready(function(){ 
    // This button will increment the value 
    $('.qtyplus').click(function(e){ 
     // Stop acting like a button 
     e.preventDefault(); 
     // Get the field name 
     fieldName = $(this).attr('field'); 
     // Get its current value 
     var currentVal = parseInt($('input[name='+fieldName+']').val()); 
     // If is not undefined 
     if (!isNaN(currentVal)) { 
      // Increment 
      $('input[name='+fieldName+']').val(currentVal + 1); 
     } else { 
      // Otherwise put a 0 there 
      $('input[name='+fieldName+']').val(0); 
     } 
    }); 
    // This button will decrement the value till 0 
    $(".qtyminus").click(function(e) { 
     // Stop acting like a button 
     e.preventDefault(); 
     // Get the field name 
     fieldName = $(this).attr('field'); 
     // Get its current value 
     var currentVal = parseInt($('input[name='+fieldName+']').val()); 
     // If it isn't undefined or its greater than 0 
     if (!isNaN(currentVal) && currentVal > 0) { 
      // Decrement one 
      $('input[name='+fieldName+']').val(currentVal - 1); 
     } else { 
      // Otherwise put a 0 there 
      $('input[name='+fieldName+']').val(0); 
     } 
    }); 
}); 
</script> 
<form id='myform' method='POST' action='#'> 
    <input type='button' value='-' class='qtyminus' field='quantity' /> 
    <input type='text' name='quantity' value='0' class='qty' /> 
    <input type='button' value='+' class='qtyplus' field='quantity' /> 
</form> 

,我想編輯的JS文件。 +按鈕應該只啓用爲4的值。換句話說,我只想顯示從0到4的值。當它是4時,它應該禁用+按鈕。我想這一點,但它不工作:

if((currentVal) < 4) { 
    $('.qtyplus').attr("disabled", false); 
} 
else { 
    $('.qtyplus').attr("disabled", true); 
} 

回答

0
jQuery(document).ready(function(){ 
    // This button will increment the value 
    $('.qtyplus').click(function(e){ 
     // Stop acting like a button 
     e.preventDefault(); 
     // Get the field name 
     fieldName = $(this).attr('field'); 
     // Get its current value 
     var currentVal = parseInt($('input[name='+fieldName+']').val()); 
     // If is not undefined 
     if (!isNaN(currentVal)) { 
      // Increment 
      $('input[name='+fieldName+']').val(currentVal + 1); 
     } else { 
      // Otherwise put a 0 there 
      $('input[name='+fieldName+']').val(0); 
     } 
     if((currentVal) > 2) { 
$('.qtyplus').attr("disabled", true); 
} 
    }); 
    // This button will decrement the value till 0 
    $(".qtyminus").click(function(e) { 
     // Stop acting like a button 
     e.preventDefault(); 
     // Get the field name 
     fieldName = $(this).attr('field'); 
     // Get its current value 
     var currentVal = parseInt($('input[name='+fieldName+']').val()); 
     // If it isn't undefined or its greater than 0 
     if (!isNaN(currentVal) && currentVal > 0) { 
      // Decrement one 
      $('input[name='+fieldName+']').val(currentVal - 1); 
     } else { 
      // Otherwise put a 0 there 
      $('input[name='+fieldName+']').val(0); 
     } 
     if((currentVal) <= 4) { 
$('.qtyplus').attr("disabled", false); 
} 
    }); 
}); 
0

即使你改變了,如果條件下,禁用屬性應除去未設置爲false,

$('.qtyplus').attr("disabled", true); 

$('.qtyplus').removeAttr("disabled"); 

爲具有任何價值殘疾人將保持禁用。