带有+/-按钮的限制值框
Quantity Box with +/- button having a limit
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);
}
});
});
如何在此框上设置限制?我不希望数量超过100,所以如果用户一直按+键,当数量达到100时,数量应该停止增加。
if(currentVal == 100)
{
$('input[name=' + fieldName + ']').val(currentVal);
}
我认为那是不对的,我需要一些帮助
编辑:http://jsfiddle.net/laelitenetwork/puJ6G/字体在这里
我创建了一个简单的逻辑,可能会对您有所帮助。由于您没有提供任何HTML,所以我制作了一个简单的结构(可能与您的结构不同):
<input type="text" name="field1" value="0" />
<button class="qty" type="button" data-func="plus" data-field="field1">+1</button>
<button class="qty" type="button" data-func="minus" data-field="field1">-1</button>
注意:
- 自定义HTML属性使用
data-
前缀,如data-field
和data-func
。 - 我给了两个按钮一个常见的
qty
类-我们可以辨别哪个按钮被点击后。这简化了选择器,也防止了多余的代码块(每个按钮一个,这很容易膨胀)。
$('.qty').click(function() {
var $t = $(this),
$in = $('input[name="'+$t.data('field')+'"]'),
val = parseInt($in.val()),
valMax = 100,
valMin = 0;
// Check if a number is in the field first
if(isNaN(val) || val < valMin) {
// If field value is NOT a number, or
// if field value is less than minimum,
// ...set value to 0 and exit function
$in.val(valMin);
return false;
} else if (val > valMax) {
// If field value exceeds maximum,
// ...set value to max
$in.val(valMax);
return false;
}
// Perform increment or decrement logic
if($t.data('func') == 'plus') {
if(val < valMax) $in.val(val + 1);
} else {
if(val > valMin) $in.val(val - 1);
}
});
很简单,是吗?您甚至可以通过在达到阈值时禁用<button>
元素来增强我的示例,但由于您没有提到它,我猜这更像是一个噱头,而不是必要的。
请参阅此处:http://jsfiddle.net/teddyrised/2aC5C/3/
在您的增量逻辑中:
if (!isNaN(currentVal) && currentVal <= 100) {
我想应该可以。
相关文章:
- 带有验证和隐藏字段值的提交按钮
- Flexslider导航按钮在悬停时不可见,带有橙色框
- 带有嵌套状态的Angular ui路由器和后退按钮
- 带有向下钻取的 Amchart 图表工作正常,但“后退”按钮不起作用
- 单击带有按钮的字段的复制值
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 带有单选按钮的html表格复制粘贴到xls中
- Rails-带有单选按钮的表单,带有启用/禁用附加元素的javascript操作
- 带有关闭按钮的javascript/jquery响应工具提示
- 带有音频标签的多个播放/暂停按钮
- 如何使用Javascript创建带有精灵表的动画按钮
- 如何使用带有动态接受好友按钮的ajax post更新更多的表
- 单击按钮即可显示带有提取的json数据的表
- 如何使用带有多个按钮的javascript更改按钮颜色
- javascript中带有启动和停止按钮的启动和停止循环
- 获取点击按钮的值/attr作为event.data,带有.on(“点击”…)
- 只有第一个按钮在带有 Jquery 的 forEach 标签中有效
- 当我单击它时,没有检查带有数据切换=选项卡的单选按钮
- 创建带有文本输入和按钮的 UL 列表
- 吸引人 - html5 导出添加了带有播放按钮的第一张幻灯片