通过jQuery在正负输入上添加“this”
Add 'this' on plus-minus inputs via jQuery
<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并且工作正常。
但是如果我在一个页面中有两个输入,一起工作。但是,它必须是分开的。
演示
我该如何解决它?
您可以使用
jquery .siblings
并选择一个输入,该输入是按钮的同级。
杰斯菲德尔
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($(this).siblings('input[name=' + fieldName + ']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$(this).siblings('input[name=' + fieldName + ']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$(this).siblings('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($(this).siblings('input[name=' + fieldName + ']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$(this).siblings('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$(this).siblings('input[name=' + fieldName + ']').val(0);
}
});
});
#myform {
text-align: center;
padding: 5px;
border: 1px dotted #ccc;
margin: 2%;
}
.qty {
width: 40px;
height: 25px;
text-align: center;
}
input.qtyplus {
width: 25px;
height: 25px;
}
input.qtyminus {
width: 25px;
height: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></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>
<form id='myform2' 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>
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- 如何在 jquery 上的设置间隔函数中添加“this”
- 将道具添加到..this.ops
- jQuery插件返回this.each并为每个对象添加函数属性
- 在原型中声明 TypeScript 成员,而不是添加到“this”中
- 在 JavaScript/TypeScript 中动态地“直接”添加对象属性到“this”
- 重构 jQuery 以使用 $(this) 并从另一个元素中添加/删除类
- 如何将锚点添加到“this.form.submit();”
- 将变量self而不是this添加到闭包中
- 通过jQuery在正负输入上添加“this”
- 使用$(this)添加不同<p>'s在一组图像的顶部
- 在 JavaScript 中使用 (this) 关键字添加新属性
- 使用jquery在“this”中添加特定元素
- 添加“this.disabled"单击以停止重复的表单提交
- JQuery用onclick()给$(this)添加一个类
- 添加(this)关键字以挑选出一个元素
- 将if添加到AJAX for Rails中(检查this是否==DOM元素)
- 如何创建一个JavaScript“类”,将方法添加到原型中,并正确使用“this”