英文:
Why button dont work in html and javascript?
问题
Increment和Decrement按钮为什么不起作用,它们之前正常工作,但当我添加了 type="button"
后就不起作用了。
英文:
Why increment and decrement buttons dont work, they was working well but when I add type="button"
stop working.
> html
<form th:action="@{/home}" method="post" style="margin-top: 10px; display: block">
<div style="margin-bottom: 10px; margin-top: 10px;">
<button type="button" name="decrement" onclick="decrement()" style="display: inline; margin-right: 5px; width: 20px" id="decrement">-</button>
<output style="display: inline" name="quantity" id="quantity">0</output>
<button type="button" name="increment" onclick="increment()" style="display: inline; margin-left: 5px; width: 20px" id="increment">+</button>
<input type="hidden" name="itemId" th:value="${item.getItem_id()}"/>
<button type="submit" name="addToCart" class="addToCart-btn">Add to cart</button>
</div>
</form>
> javascript
function increment(button) {
var counter = button.parentNode.querySelector("output");
counter.textContent++;
updateAddToCartButton(button)
}
function decrement(button) {
var counter = button.parentNode.querySelector("output");
if (counter.textContent > 0) {
counter.textContent--;
updateAddToCartButton(button)
}
}
function updateAddToCartButton(button) {
var quantity = parseInt(button.parentNode.querySelector("#quantity").innerText);
var submitButton = button.parentNode.querySelector(".addToCart-btn");
if (quantity > 0) {
submitButton.style.display = "inline";
} else {
submitButton.style.display = "none";
}
}
答案1
得分: 0
The main issue is that you are using inline event handlers. In this context, increment
refers to the button with the id of increment
rather than the function. You also pass no argument to increment
and decrement
so button
is undefined
in both.
Use addEventListener
with event.target
instead.
document.querySelector('#increment').addEventListener('click', e => increment(e.target));
document.querySelector('#decrement').addEventListener('click', e => decrement(e.target));
function increment(button) {
var counter = button.parentNode.querySelector("output");
counter.textContent++;
updateAddToCartButton(button)
}
function decrement(button) {
var counter = button.parentNode.querySelector("output");
if (counter.textContent > 0) {
counter.textContent--;
updateAddToCartButton(button)
}
}
function updateAddToCartButton(button) {
var quantity = parseInt(button.parentNode.querySelector("#quantity").innerText);
var submitButton = button.parentNode.querySelector(".addToCart-btn");
if (quantity > 0) {
submitButton.style.display = "inline";
} else {
submitButton.style.display = "none";
}
}
<form th:action="@{/home}" method="post" style="margin-top: 10px; display: block">
<div style="margin-bottom: 10px; margin-top: 10px;">
<button type="button" name="decrement" style="display: inline; margin-right: 5px; width: 20px" id="decrement">-</button>
<output style="display: inline" name="quantity" id="quantity">0</output>
<button type="button" name="increment" style="display: inline; margin-left: 5px; width: 20px" id="increment">+</button>
<input type="hidden" name="itemId" th:value="${item.getItem_id()}"/>
<button type="submit" name="addToCart" class="addToCart-btn">Add to cart</button>
</div>
</form>
英文:
The main issue is that you are using inline event handlers. In this context, increment
refers to the button with the id of increment
rather than the function. You also pass no argument to increment
and decrement
so button
is undefined
in both.
Use addEventListener
with event.target
instead.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelector('#increment').addEventListener('click', e => increment(e.target));
document.querySelector('#decrement').addEventListener('click', e => decrement(e.target));
function increment(button) {
var counter = button.parentNode.querySelector("output");
counter.textContent++;
updateAddToCartButton(button)
}
function decrement(button) {
var counter = button.parentNode.querySelector("output");
if (counter.textContent > 0) {
counter.textContent--;
updateAddToCartButton(button)
}
}
function updateAddToCartButton(button) {
var quantity = parseInt(button.parentNode.querySelector("#quantity").innerText);
var submitButton = button.parentNode.querySelector(".addToCart-btn");
if (quantity > 0) {
submitButton.style.display = "inline";
} else {
submitButton.style.display = "none";
}
}
<!-- language: lang-html -->
<form th:action="@{/home}" method="post" style="margin-top: 10px; display: block">
<div style="margin-bottom: 10px; margin-top: 10px;">
<button type="button" name="decrement" style="display: inline; margin-right: 5px; width: 20px" id="decrement">-</button>
<output style="display: inline" name="quantity" id="quantity">0</output>
<button type="button" name="increment" style="display: inline; margin-left: 5px; width: 20px" id="increment">+</button>
<input type="hidden" name="itemId" th:value="${item.getItem_id()}"/>
<button type="submit" name="addToCart" class="addToCart-btn">Add to cart</button>
</div>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论