我有:
我有一個只讀文本框和兩個鏈接。而第二鏈路進行1jQuery:如果值等於0,則停止遞減文本框值
遞減值第一個鏈接遞增1的文本框的值,我需要什麼:
我需要的文本框的值停在零遞減(我不想負號)。
我的代碼:
的jQuery:
jQuery(".increment").on('click',function(){
jQuery(this).next(".amount input").val(parseInt(jQuery(this).next(".amount input").val())+1);
});
jQuery(".decrement").on('click',function(){
jQuery(this).prev(".amount input").val(parseInt(jQuery(this).prev(".amount input").val())-1);
});
注:。接下來和.prev的使用,因爲我有多個文本框。
HTML:
<!--This block of code is occurs more than once.-->
<td class="amount">
<a href="#" class="increment"></a>
<input type="text" readonly="readonly" value="0" />
<a href="#" class="decrement"></a>
</td>
鬆動的猜測:
...至於遞減...
// open onclick code here
if (jQuery(this).prev(".amount input").val != "0"){
// decrement code here
}
// close onclick code here
資源:
我的工作代碼模擬此堆棧溢出回答:https://stackoverflow.com/a/12209831/835950
...在哪個小提琴存在方便:http://jsfiddle.net/iMaverick/S3PLG/2/
感謝您的解決方案。當我整合.next和.prev時,這很容易工作。 – 2013-04-22 05:34:58
爲'parseInt'指定一個基數。 – Blender 2013-04-22 05:53:52
謝謝,我忘記了..它的真正意義..謝謝指出。 – PSL 2013-04-22 05:56:01