使这个增量/减量与 jQuery 中的多个输入一起工作

发布于 2024-12-04 14:23:10 字数 1211 浏览 7 评论 0原文

我在选择输入时遇到了一些麻烦。基本上,我需要按钮来增加或减少同一元素中包含的单个输入的值。

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最丧也最甜 2024-12-11 14:23:10
incrementVar = 0;
$('.inc.button').click(function(){
    var $this = $(this),
        $input = $this.prev('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val(), 10)+1;
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});
$('.dec.button').click(function(){
    var $this = $(this),
        $input = $this.next('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val(), 10)-1;
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});

演示

incrementVar = 0;
$('.inc.button').click(function(){
    var $this = $(this),
        $input = $this.prev('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val(), 10)+1;
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});
$('.dec.button').click(function(){
    var $this = $(this),
        $input = $this.next('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val(), 10)-1;
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});

Demo

静若繁花 2024-12-11 14:23:10

这是一些可以为您完成此操作的代码。关键是转到所单击按钮的父级并找到该父级中的输入。这将为您找到合适的手术对象。我还进行了这些更改/改进:

  1. 我将所有代码放在递增和递减都可以调用的函数中。
  2. 我将基数添加到了 parseInt() 中,除非您希望它猜测,否则这是必需的。
  3. 我这样做是为了使减量不能使其低于零。
  4. 我将输入元素上的 id 值修复为合法值(id 不能以数字开头)。

这是新代码:

$(".inc").click(function() {
    updateValue(this, 1);
});
$(".dec").click(function() {
    updateValue(this, -1);
});

function updateValue(obj, delta) {
    var item = $(obj).parent().find("input");
    var newValue = parseInt(item.val(), 10) + delta;
    item.val(Math.max(newValue, 0));
}

这里的工作示例: 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:

  1. I put all the code in a function that both increment and decrement can call.
  2. I added the radix to parseInt() which is required unless you want it to guess.
  3. I made it so the decrement can't make it go below zero.
  4. I fixed the id values on the input elements to be legal values (an id can't start with an number).

Here's the new code:

$(".inc").click(function() {
    updateValue(this, 1);
});
$(".dec").click(function() {
    updateValue(this, -1);
});

function updateValue(obj, delta) {
    var item = $(obj).parent().find("input");
    var newValue = parseInt(item.val(), 10) + delta;
    item.val(Math.max(newValue, 0));
}

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.

辞别 2024-12-11 14:23:10

您可以使用 prev()next() 查找元素,而不是使用选择器。

查看实际操作:http://jsfiddle.net/william/t4Np7/1/

You can use the prev() and next() the find the element, instead of using selector.

See it in action: http://jsfiddle.net/william/t4Np7/1/.

层林尽染 2024-12-11 14:23:10

成年女性部分内的输入元素名为 adult-female,而不是 qty。您可以调整选择器以使用 jQuery 的 next ala $(this).next(":input")

Your input element inside the adult female section is named adult-female and not qty. You can adjust your selector to use jQuery's next ala $(this).next(":input").

水波映月 2024-12-11 14:23:10

您可以使用:

var input = $(this).siblings(":input[name='qty']").get(0)

然后

var newValue = parseInt(input.value) + 1;
$(input).val(newValue);

You can use :

var input = $(this).siblings(":input[name='qty']").get(0)

and then

var newValue = parseInt(input.value) + 1;
$(input).val(newValue);
陌上青苔 2024-12-11 14:23:10

使用

 var newValue = parseInt( $(this).prev(":input[name='qty']").val()) + 1;

而不是

 var newValue = parseInt($(":input[name='qty']").val()) + 1;

使用

 var newValue = parseInt( $(this).next(":input[name='qty']").val()) - 1;

而不是

 var newValue = parseInt($(":input[name='qty']").val()) - 1;

参考:

Use

 var newValue = parseInt( $(this).prev(":input[name='qty']").val()) + 1;

instead of

 var newValue = parseInt($(":input[name='qty']").val()) + 1;

and

use

 var newValue = parseInt( $(this).next(":input[name='qty']").val()) - 1;

instead of

 var newValue = parseInt($(":input[name='qty']").val()) - 1;

Ref:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文