简体   繁体   English

次要onClick事件不起作用

[英]Secondary onClick event not working

I have 3 click events on the page by default (blue X, blue +, grey X). 默认情况下,我在页面上有3个点击事件(蓝色X,蓝色+,灰色X)。 Each one opens a modal box. 每个打开一个模式框。

在此处输入图片说明

Each of those modal boxes, has a button. 每个模式框都有一个按钮。 Two of the modal boxes, the one for the blue X, and the one for the blue +, both have functioning buttons inside. 模态框中的两个,一个用于蓝色X,一个用于蓝色+,都具有有效的按钮。 The console message shows for both of these additional click events when I click their respective buttons. 当我单击它们各自的按钮时,控制台消息将同时显示这两个附加单击事件。 I click the button in the modal for the blue X or blue +, and I get the console log message. 单击模式中蓝色X或蓝色+的按钮,我收到控制台日志消息。 Good. 好。 But this doesn't work fo the grey X. 但这对于灰色X无效。

The other 3rd modal box does open, but the button inside doesn't work. 另一个第三个模式框确实打开了,但是里面的按钮不起作用。 This button has an id= 此按钮的ID =

remPostConfirm

The code for this button that doesn't' work is: 此按钮无效的代码是:

showRemPost = function(id, url, lname) {
    console.log("remPost1");
    if (isAuth) {
        $('#remPostConfirm').off('click').on("click", function(e) {
            e.preventDefault();
            console.log("remPost2");
        });
        $('#remPostModal .description').html('<p>Post: ' + url + '</p><p>From List: ' + lname + '</p><div  id="remListConfirm" class="mini ui button">Remove</div >');
        $('#remPostModal').modal('show');
    } else {
        deniedAccess();
    }
}

The other two buttons that do work, have essentially the same code. 起作用的其他两个按钮具有基本相同的代码。

I am lost as to why this no longer works, as this 3rd modal button used to work. 我迷失了为什么它不再起作用,因为这第三个模式按钮曾经起作用。 If you try to click on the grey X for this modal to load, then on the "Remove" button, you won't get a console log message like the other two give (the blue X and blue +). 如果您尝试单击灰色的X以便加载此模式,则在“删除”按钮上,您将不会像其他两个一样收到控制台日志消息(蓝色X和蓝色+)。

Here is a jsfiddle example for you test and see. 这是一个供您测试和查看的jsfiddle示例。

https://jsfiddle.net/qrmrfrhj/16/ https://jsfiddle.net/qrmrfrhj/16/

Sorry for the JS being included in the HTML, I can't get the example to work if I separate it down below. 很抱歉,HTML中包含了JS,如果我在下面将其分开,则无法使该示例正常工作。 If you want to help me get that working before downvoting me, that would be nice. 如果您想帮助我在降低我的票数之前让它正常工作,那就太好了。 Thanks. 谢谢。

Thank you for any help. 感谢您的任何帮助。 Peace. 和平。

You have two problems in showRemPost . showRemPost有两个问题。

First, in the HTML that you add to #remPostModal , you have id="remListConfirm" , but you're adding a click handler on #remPostConfirm . 首先,在您添加到#remPostModal的HTML中,您具有id="remListConfirm" ,但是您要在#remPostConfirm上添加一个点击处理程序。 remListConfirm is already used in remListModal , you shouldn't duplicate it here. remListConfirm已在remListModal ,您不应在此处重复。

Second, you bind the click handler before you create the button with .html() . 其次,在使用.html()创建按钮之前,请绑定单击处理程序。 So when you create the button, it doesn't have a click handler. 因此,当您创建按钮时,它没有单击处理程序。 You need to change the order. 您需要更改顺序。 There's no need for .off() , since a brand new element won't have a click handler. 不需要.off() ,因为一个崭新的元素将没有点击处理程序。 Alternatively you could use event delegation. 或者,您可以使用事件委托。 Or you could do it like the other dialogs, where you don't overwrite the button that's in the original HTML. 或者,您也可以像其他对话框一样进行操作,在该对话框中,您不会覆盖原始HTML中的按钮。

  showRemPost = function(id, url, lname) {
    console.log("remPost1");
    if (isAuth) {
      $('#remPostModal .description').html('<p>Post: ' + url + '</p><p>From List: ' + lname + '</p><div  id="remPostConfirm" class="mini ui button">Remove</div >');
      $('#remPostConfirm').on("click", function(e) {
        e.preventDefault();
        console.log("remPost2");
      });

      $('#remPostModal').modal('show');
    } else {
      deniedAccess();
    }
  }

You have two issues: 您有两个问题:

  1. The id of the "remove" button in your popup is actually remListConfirm , not remPostConfirm 弹出窗口中“删除”按钮的ID实际上是remListConfirm ,而不是remPostConfirm
  2. You're creating your events BEFORE you add the button to the DOM. 在将按钮添加到DOM之前,您要创建事件。 That means when JS looks for an element with the id 'remListConfirm', it's going to find nothing and thus not attach the event to anything. 这意味着当JS查找ID为'remListConfirm'的元素时,它将什么也找不到,因此不会将事件附加到任何东西上。

The solution is to change the id in your JS to remListConfirm and move the .html line ABOVE the lines where you create the click events. 解决方案是将JS中的ID更改为remListConfirm并将.html行移到创建click事件的行的remListConfirm Here's the section fixed: 这是固定的部分:

                 if (isAuth) {
                        $('#remPostModal .description').html('<p>Post: ' + url + '</p><p>From List: ' + lname + '</p><div  id="remListConfirm" class="mini ui button">Remove</div >');
                    $('#remListConfirm').off('click').on("click", function(e) {
                        e.preventDefault();
                        console.log("remPost2");
                    });

                    $('#remPostModal').modal('show');
                } else {
                    deniedAccess();
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM