简体   繁体   English

当其他函数创建的元素调用事件时,.click函数不起作用

[英].click function not working when the event is called by element created by another function

I have this function that create an element 我有这个功能创建一个元素

function AddPayment(ID)
{
    showForm = $('#AddPaymentForm').html();    
    $('#divAJAX_'+ID).html(showForm);  

    $(".cancel").click(function(){ AddPayment(ID); });
}

Coming from this 来自这个

<div id='AddPaymentForm'>    
       <span class='button' id='cancel' class='cancel' >Cancel</span>
</div>

I wanted that function to place the element in here 我希望该功能将元素放在这里

<div id='divAJAX_ID'></div>

I also wanted that function to create an onclick function on my span, but it isn't working. 我还希望该函数在我的跨度上创建一个onclick函数,但是它不起作用。 I guess the problem is coming from placing the 我猜问题出在

$(".cancel").click(function(){ AddPayment(ID); });

at the wrong placement. 放置位置错误。 I've tried all the possible place but I can't still work this right. 我已经尝试了所有可能的地方,但是仍然不能正确地工作。

What's wrong? 怎么了?

You have two class attributes on the same element. 您在同一元素上有两个类属性 It should be something like: 应该是这样的:

class="button cancel"

instead of 代替

class="button" id="whatever" class="cancel"

It is probably causing trouble to jQuery. 这可能给jQuery造成麻烦。

See how it start working on this fiddle: http://jsfiddle.net/pvNrg/2/ 查看它如何开始在此提琴上工作: http : //jsfiddle.net/pvNrg/2/

First, your question as the html: 首先,将您的问题作为html:

<div id='AddPaymentForm'>
    <p>Coming from this</p>
</div>

<span id='cancel' class='cancel'>Cancel</span>

<p>I wanted that function to place the element in here</p>

<div id='divAJAX_ID'></div>

<p>I also wanted that function to create an onclick function on my span, but it isn't working. I guess the problem is coming from placing the ... at the wrong placement. I've tried all the possible place but I can't still work this right.</p>

<p>What's wrong?</p>

And the javascript: 和javascript:

$(function () {

    function AddPayment(ID) {
        showForm = $('#AddPaymentForm').html();
        $('#divAJAX_' + ID).html(showForm);
    }

    $(".cancel").click(function () {
        AddPayment('ID');
    });

}); });

For dynamically created elements ,You have to do event delegation 对于动态创建的元素,您必须执行事件委托

$(document).on("click", ".cancel" , function(event){
  alert($(this).text());
});
$("#AddPaymentForm,[id^=divAJAX_]").on("click", ".cancel" , function(event){
  alert($(this).text());
});

Attach the delegated event handlers only the list of containers you need to monitor (not the document) if you need to save performance (you don't need to monitor all the .cancel ). 如果需要保存性能(不需要监视所有.cancel ),则仅将委派的事件处理程序仅附加需要监视的容器列表(而不是文档)。 With this approach, the event does not have to bubble up more levels. 使用这种方法,事件不必冒出更多的泡沫。 If you have anther element that is parent of AddPaymentForm and all divAJAX_ . 如果您有AddPaymentForm元素是AddPaymentForm和所有divAJAX_父元素。 You could write like this: 您可以这样写:

$("#anotherContainer").on("click", ".cancel" , function(event){
      alert($(this).text());
    });

Use this code 使用此代码

function AddPayment(ID)
{
   var showForm = $('#AddPaymentForm').html();
   var container = $('#divAJAX_'+ID);

   container.html(showForm);
   container.find(".cancel").click(function(){ AddPayment(ID); });
}

暂无
暂无

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

相关问题 当事件被另一个函数的元素调用时,JavaScript函数不起作用 - Javascript function not working when the event is called by element from another function 调用事件时函数不起作用 - Function not working when event is called 单击函数无法像从函数调用时那样工作 - Click function not working as when called from a function 当在javascript函数内部动态创建div时,jquery的click事件不起作用,但在页面加载时它起作用 - Click event of jquery is not working when div is dynamically created inside javascript function except on page load it is working 定位功能中动态创建的HTML元素(用于追加事件-不能单击!)jQuery - Target dynamically created HTML element in function (for append event— not click!) jQuery 将单击事件函数绑定到动态创建的元素 - Bind click event function to a dynamically-created element 如何以编程方式单击元素并将单击事件发送到另一个 function? - How to programmatically click element and send the click event to another function? 通过另一个函数调用时,clearTimeout()不起作用 - clearTimeout() is not working when called via another function 为什么 Bootstrap 4 轮播暂停功能在从轮播内元素的事件中被调用时不起作用? - Why is Bootstrap 4 carousel pause function not working when it's getting called from the event of an element inside of the carousel? 在另一个函数中创建Mongoose连接时不调用查询回调 - Query Callback Not Called When Mongoose Connection Created In Another Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM