使这个增量/减量与 jQuery 中的多个输入一起工作
我在选择输入时遇到了一些麻烦。基本上,我需要按钮来增加或减少同一元素中包含的单个输入的值。
这是 HTML:
<div>
<label for="name">Adult Males</label>
<div class="dec button">-</div>
<input type="text" name="qty" id="1" value="0" />
<div class="inc button">+</div>
</div>
<div>
<label for="name">Adult Females</label>
<div class="dec button">-</div>
<input type="text" name="adult-female" id="2" value="0"/>
<div class="inc button">+</div>
</div>
...和 jQuery:
var incrementVar = 0;
$(function() {
$(".inc").click(function() {
var newValue = parseInt($(":input[name='qty']").val()) + 1;
$(":input[name='qty']").val(newValue);
$('.inc').addClass('a' + newValue);
incrementVar = incrementVar + newValue;
});
$(".dec").click(function() {
var newValue = parseInt($(":input[name='qty']").val()) - 1;
$(":input[name='qty']").val(newValue);
$('.inc').addClass('a' + newValue);
incrementVar = incrementVar + newValue;
});
});
这有效,但目前仅适用于成年男性。我希望按钮能够定位同一 div 中包含的输入。
I am having a bit of trouble selecting an input. Basically, I need the button to increase or decrease the value of a single input contained in the same element.
Here's the HTML:
<div>
<label for="name">Adult Males</label>
<div class="dec button">-</div>
<input type="text" name="qty" id="1" value="0" />
<div class="inc button">+</div>
</div>
<div>
<label for="name">Adult Females</label>
<div class="dec button">-</div>
<input type="text" name="adult-female" id="2" value="0"/>
<div class="inc button">+</div>
</div>
...and the jQuery:
var incrementVar = 0;
$(function() {
$(".inc").click(function() {
var newValue = parseInt($(":input[name='qty']").val()) + 1;
$(":input[name='qty']").val(newValue);
$('.inc').addClass('a' + newValue);
incrementVar = incrementVar + newValue;
});
$(".dec").click(function() {
var newValue = parseInt($(":input[name='qty']").val()) - 1;
$(":input[name='qty']").val(newValue);
$('.inc').addClass('a' + newValue);
incrementVar = incrementVar + newValue;
});
});
This works, but only for adult-males at the moment. I want the button to target the input contained in the same div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
演示
Demo
这是一些可以为您完成此操作的代码。关键是转到所单击按钮的父级并找到该父级中的输入。这将为您找到合适的手术对象。我还进行了这些更改/改进:
parseInt()
中,除非您希望它猜测,否则这是必需的。这是新代码:
这里的工作示例: http://jsfiddle.net/jfriend00/Y6tFj/
I删除了类更改,因为它们对我来说没有意义,因为您不断向所有增量/减量按钮添加新类,我认为这无法完成您想要做的任何事情。
我也不知道你想用incrementVar 做什么。
Here's some code that will do that for you. The key is to go up to the parent of the button that's clicked and find the input that's in that parent. That will get you the right one to operate on. I also made these changes/improvements:
parseInt()
which is required unless you want it to guess.Here's the new code:
Working example here: http://jsfiddle.net/jfriend00/Y6tFj/
I removed the class changes as they didn't make sense to me because you're constantly adding new classes to all the increment/decrement buttons which I don't think is accomplishing whatever you were trying to do.
I also didn't know what you were trying to do with the incrementVar.
您可以使用
prev()
和next()
查找元素,而不是使用选择器。查看实际操作:http://jsfiddle.net/william/t4Np7/1/。
You can use the
prev()
andnext()
the find the element, instead of using selector.See it in action: http://jsfiddle.net/william/t4Np7/1/.
成年女性部分内的输入元素名为
adult-female
,而不是qty
。您可以调整选择器以使用 jQuery 的next
ala$(this).next(":input")
。Your input element inside the adult female section is named
adult-female
and notqty
. You can adjust your selector to use jQuery'snext
ala$(this).next(":input")
.您可以使用:
var input = $(this).siblings(":input[name='qty']").get(0)
然后
You can use :
var input = $(this).siblings(":input[name='qty']").get(0)
and then
使用
而不是
和
使用
而不是
参考:
.prev 的文档()
.next()
Use
instead of
and
use
instead of
Ref:
Documentation for .prev()
Documentation for .next()