[英].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.