首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输入数字中箭头的Javascript实现-如何进行更改以使每一项都相关?

输入数字中箭头的Javascript实现-如何进行更改以使每一项都相关?
EN

Stack Overflow用户
提问于 2019-07-03 23:26:56
回答 2查看 141关注 0票数 0

我有一篮子商品,里面可以有许多不同的实体。我正在尝试使用不带箭头的input - number和额外的按钮+/-来实现商品数量的变化。我知道我可以使用identifiers并轻松地制定我的计划。但在我的例子中,我需要使用querySelectorAll。请帮我改正这段代码。quantity-arrow-minus会减少字段值,而quantity-arrow-plus会增加。如何才能使更改与每一项都相关?

代码语言:javascript
复制
var minus = document.querySelectorAll('.quantity-arrow-minus');
var update_cart = document.querySelectorAll('.update_cart');
var plus = document.querySelectorAll('.quantity-arrow-plus');

minus.forEach(function(node) {
node.addEventListener('click', function() {
    update_cart.forEach(function(element) {
        element.value = parseInt(element.value) - 1;
    });
});
});

plus.forEach(function(node) {
node.addEventListener('click', function() {
    update_cart.forEach(function(element) {
        element.value = parseInt(element.value) + 1;
    });
});
});
代码语言:javascript
复制
<form method="GET">
<button type="button" class="quantity-arrow-minus">-</button>
<input type="number" class="update_cart" value="1">
<button type="button" class="quantity-arrow-plus">+</button>
</form>

<form method="GET">
<button type="button" class="quantity-arrow-minus">-</button>
<input type="number" class="update_cart" value="1">
<button type="button" class="quantity-arrow-plus">+</button>
</form>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-03 23:48:35

您可以使用previousElementSibling and nextElementSibling访问与所单击的按钮相对应的输入:

代码语言:javascript
复制
var minus = document.querySelectorAll('.quantity-arrow-minus');
var update_cart = document.querySelectorAll('.update_cart');
var plus = document.querySelectorAll('.quantity-arrow-plus');

minus.forEach(function(node) {
    node.addEventListener('click', function(e) {
       const input = e.target.nextElementSibling
       input.value = parseInt(input.value) - 1;
    });
});

plus.forEach(function(node) {
    node.addEventListener('click', function(e) {
       const input = e.target.previousElementSibling
       input.value = parseInt(input.value) + 1;
    });
});
代码语言:javascript
复制
<form action="{% url 'cart:cart_update' %}" method="GET">
    <button type="button" class="quantity-arrow-minus">-</button>
    <input type="number" class="update_cart" value="0">
    <button type="button" class="quantity-arrow-plus">+</button>
    <br>
    <button type="button" class="quantity-arrow-minus">-</button>
    <input type="number" class="update_cart" value="0">
    <button type="button" class="quantity-arrow-plus">+</button>
</form>

票数 1
EN

Stack Overflow用户

发布于 2019-07-03 23:47:51

请改用Number()。假设减号按钮就在您的输入之后,只需使用this.nextElementSibling即可。这将使您的代码更好,而不是对每个元素执行forEach。如果有很多这样的元素呢?

代码语言:javascript
复制
var minus = document.querySelectorAll('.quantity-arrow-minus');
var plus = document.querySelectorAll('.quantity-arrow-plus');

minus.forEach((node) => {
   node.onclick = function () {
       this.nextElementSibling.value = Number(this.nextElementSibling.value) - 1;
   }
});

plus.forEach((node) => {
    node.onclick = function () {
       this.previousElementSibling.value =Number(this.previousElementSibling.value) + 1;
   }
});
代码语言:javascript
复制
<button class="quantity-arrow-minus">-</button>
<input type="number" class="update_cart" value="1">
<button class="quantity-arrow-plus">+</button>
<br>
<button class="quantity-arrow-minus">-</button>
<input type="number" class="update_cart" value="1">
<button class="quantity-arrow-plus">+</button>
<br>
<button class="quantity-arrow-minus">-</button>
<input type="number" class="update_cart" value="1">
<button class="quantity-arrow-plus">+</button>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56873470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档