为什么HTML和JavaScript中的按钮不起作用?

huangapple go评论71阅读模式
英文:

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(&#39;#increment&#39;).addEventListener(&#39;click&#39;, e =&gt; increment(e.target));
document.querySelector(&#39;#decrement&#39;).addEventListener(&#39;click&#39;, e =&gt; decrement(e.target));

function increment(button) {
    var counter = button.parentNode.querySelector(&quot;output&quot;);
    counter.textContent++;
    updateAddToCartButton(button)
}

function decrement(button) {
    var counter = button.parentNode.querySelector(&quot;output&quot;);
    if (counter.textContent &gt; 0) {
        counter.textContent--;
        updateAddToCartButton(button)
    }
}

function updateAddToCartButton(button) {
    var quantity = parseInt(button.parentNode.querySelector(&quot;#quantity&quot;).innerText);
    var submitButton = button.parentNode.querySelector(&quot;.addToCart-btn&quot;);
    if (quantity &gt; 0) {
        submitButton.style.display = &quot;inline&quot;;
    } else {
        submitButton.style.display = &quot;none&quot;;
    }
}

<!-- language: lang-html -->

&lt;form th:action=&quot;@{/home}&quot; method=&quot;post&quot; style=&quot;margin-top: 10px; display: block&quot;&gt;
      &lt;div style=&quot;margin-bottom: 10px; margin-top: 10px;&quot;&gt;
          &lt;button type=&quot;button&quot; name=&quot;decrement&quot; style=&quot;display: inline; margin-right: 5px; width: 20px&quot; id=&quot;decrement&quot;&gt;-&lt;/button&gt;
          &lt;output style=&quot;display: inline&quot; name=&quot;quantity&quot; id=&quot;quantity&quot;&gt;0&lt;/output&gt;
          &lt;button type=&quot;button&quot; name=&quot;increment&quot; style=&quot;display: inline; margin-left: 5px; width: 20px&quot; id=&quot;increment&quot;&gt;+&lt;/button&gt;
          &lt;input type=&quot;hidden&quot; name=&quot;itemId&quot; th:value=&quot;${item.getItem_id()}&quot;/&gt;
          &lt;button type=&quot;submit&quot; name=&quot;addToCart&quot; class=&quot;addToCart-btn&quot;&gt;Add to cart&lt;/button&gt;
       &lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 07:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定