1. <tfoot id='mkfwp'></tfoot>

        <small id='mkfwp'></small><noframes id='mkfwp'>

        <legend id='mkfwp'><style id='mkfwp'><dir id='mkfwp'><q id='mkfwp'></q></dir></style></legend>
          <bdo id='mkfwp'></bdo><ul id='mkfwp'></ul>
        <i id='mkfwp'><tr id='mkfwp'><dt id='mkfwp'><q id='mkfwp'><span id='mkfwp'><b id='mkfwp'><form id='mkfwp'><ins id='mkfwp'></ins><ul id='mkfwp'></ul><sub id='mkfwp'></sub></form><legend id='mkfwp'></legend><bdo id='mkfwp'><pre id='mkfwp'><center id='mkfwp'></center></pre></bdo></b><th id='mkfwp'></th></span></q></dt></tr></i><div id='mkfwp'><tfoot id='mkfwp'></tfoot><dl id='mkfwp'><fieldset id='mkfwp'></fieldset></dl></div>

        按钮 onclick 函数触发两次

        时间:2023-09-05

        <small id='ndXeh'></small><noframes id='ndXeh'>

        <legend id='ndXeh'><style id='ndXeh'><dir id='ndXeh'><q id='ndXeh'></q></dir></style></legend>
      2. <tfoot id='ndXeh'></tfoot>
          <bdo id='ndXeh'></bdo><ul id='ndXeh'></ul>
                <tbody id='ndXeh'></tbody>

                • <i id='ndXeh'><tr id='ndXeh'><dt id='ndXeh'><q id='ndXeh'><span id='ndXeh'><b id='ndXeh'><form id='ndXeh'><ins id='ndXeh'></ins><ul id='ndXeh'></ul><sub id='ndXeh'></sub></form><legend id='ndXeh'></legend><bdo id='ndXeh'><pre id='ndXeh'><center id='ndXeh'></center></pre></bdo></b><th id='ndXeh'></th></span></q></dt></tr></i><div id='ndXeh'><tfoot id='ndXeh'></tfoot><dl id='ndXeh'><fieldset id='ndXeh'></fieldset></dl></div>
                • 本文介绍了按钮 onclick 函数触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个使用事件处理程序调用 javascript 函数的按钮.由于某种原因,事件处理程序被调用了两次.

                  I have a button that calls a javascript function using an event handler. For some reason, the event handler is being called twice.

                  这是我的按钮(我使用 php 对象来生成代码,这就是为什么有很多空标签的原因):

                  Here is my button (I am using a php object to generate the code, that's why there are a lot of empty tags):

                  <button name="addToCart" value="" size="" onclick="" src="" class="addToCartButton" id="0011110421111" type="button" formtarget="_self" formmethod="post" formaction="" data-mini="true" width="" height="" placeholder="" data-mini="1" onkeypress="" >Add To Cart</button>
                  

                  这是我的事件处理程序:

                  Here is my event handler:

                  $('.addToCartButton').click(function() {
                      alert("bob");
                      //addToCart($(this).attr("id"));
                  });
                  

                  在这里,我收到了两次警报.

                  Here, I am getting the alert twice.

                  我曾尝试在按钮的 onclick 属性中调用函数 addToCart,但如果我这样尝试,我会收到此错误:

                  I have tried calling the function addToCart in the button's onclick property, but if I try it that way, I get this error:

                  TypeError: '[object HTMLButtonElement]' is not a function (evaluating 'addToCart(0011110421111)')
                  

                  我也尝试过 event.preventDefault() 和 event.stopPropagation(),但都没有成功.

                  I have also tried event.preventDefault() and event.stopPropagation(), and neither worked.

                  任何想法为什么会发生这种情况,或者我可以做些什么来阻止它执行两次,或者如果我从 onclick="" 调用 javascript 函数可能会出现错误?

                  Any ideas why this is happening, or what I can do to stop it from executing twice, or maybe why I am getting an error if I call the javascript function from onclick=""?

                  推荐答案

                  也许你在同一个按钮上附加了两次事件.您可以做的是取消绑定任何以前设置的点击事件,如下所示:

                  Maybe you are attaching the event twice on the same button. What you could do is unbind any previously set click events like this:

                  $('.addToCartButton').unbind('click').click(function() {
                      alert("bob");
                      //addToCart($(this).attr("id"));
                  });
                  

                  这适用于所有附加事件(鼠标悬停、鼠标悬停、单击...)

                  This works for all attached events (mouseover, mouseout, click, ...)

                  这篇关于按钮 onclick 函数触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:javascript click 事件处理程序在不点击的情况下触发 下一篇:jQuery .on() 方法看不到新元素

                  相关文章

                  最新文章

                • <legend id='IXk3N'><style id='IXk3N'><dir id='IXk3N'><q id='IXk3N'></q></dir></style></legend>

                    <bdo id='IXk3N'></bdo><ul id='IXk3N'></ul>
                  <i id='IXk3N'><tr id='IXk3N'><dt id='IXk3N'><q id='IXk3N'><span id='IXk3N'><b id='IXk3N'><form id='IXk3N'><ins id='IXk3N'></ins><ul id='IXk3N'></ul><sub id='IXk3N'></sub></form><legend id='IXk3N'></legend><bdo id='IXk3N'><pre id='IXk3N'><center id='IXk3N'></center></pre></bdo></b><th id='IXk3N'></th></span></q></dt></tr></i><div id='IXk3N'><tfoot id='IXk3N'></tfoot><dl id='IXk3N'><fieldset id='IXk3N'></fieldset></dl></div>
                • <tfoot id='IXk3N'></tfoot>

                  <small id='IXk3N'></small><noframes id='IXk3N'>