私が持っているもの:
読み取り専用のテキスト ボックスと 2 つのリンクがあります。最初のリンクはテキストボックスの値を 1 増やしますが、2 番目のリンクは値を 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);
});
注: 複数のテキスト ボックスがあるため、.next と .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/