jQuery:如果值等于0,则停止递减文本框值 [英] jQuery: Stop decrementing textbox value if value equals 0

查看:142
本文介绍了jQuery:如果值等于0,则停止递减文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:



我有一个只读文本框和两个链接。第一个链接将文本框的值增加1,而第二个链接将值减1。



我需要的:



我需要文本框的值停止递减为零(我不想要负数)。



我的代码:



jQuery:
$ b

  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这个).prev(。amount input).val()) - 1);
});

注意:使用.next和.prev是因为我有多个文本框。



HTML:
$ b

 < ;! - 这个代码块不止一次出现.--> 
< td class =amount>
< a href =#class =increment>< / a>
< input type =textreadonly =readonlyvalue =0/>
< a href =#class =decrement>< / a>
< / td>



宽松猜测:



.. ...关于递减...

  //在这里打开onclick代码

if(jQuery( this).prev(。amount input)。val!=0){

//递减代码

}

// close onclick code here



资源:



我的工作代码是模拟这个堆栈溢出答案: https://stackoverflow.com/a/12209831/835950

...为了方便,小提琴存在: http://jsfiddle.net/iMaverick/S3PLG/2/

解决方案

Demo

  $( (); $($'$'); $($'$'); $($'$' #incdec输入 ).val((value-1)< 0? 0:(值-1)); 
});


What I have:

I have a readonly textbox and two links. The first link increments the value of the textbox by 1 whereas the second link decrements the value by 1.

What I need:

I need the textbox value to stop decrementing at zero (I don't want negative numbers).

My code:

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);
});

Note: .next and .prev are used because I have multiple textboxes.

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>

A loose guess:

...with regards to decrementing...

// open onclick code here

    if (jQuery(this).prev(".amount input").val != "0"){

        // decrement code here

    }

// close onclick code here

Resources:

My working code is modelled on this Stack Overflow answer: https://stackoverflow.com/a/12209831/835950

...at which a fiddle exists for convenience: http://jsfiddle.net/iMaverick/S3PLG/2/

解决方案

Demo

$("#down").on('click',function(){
    var value = (parseInt($("#incdec input").val() , 10) - 1);
    $("#incdec input").val((value-1) < 0 ? 0 :(value -1));
});

这篇关于jQuery:如果值等于0,则停止递减文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆