jquery 以编程方式单击新的 dom 元素
我正在尝试在 jquery 中做一些棘手的事情(至少对我来说)。我有一个复选框绑定到一个名为 add_course 的函数,如下所示:
function add_course(){
var id = $(this).attr('id');
if ($(this).is(':checked')){
if($('#courseInput').val().search(id) < 0){
$('#CourseSummary').append('<span id="cn'+id+'"><a href="#" class="courseDel" id="'+id+'">X</a> '+$('#course'+id+' a').html()+'<br/></span>');
$('#courseInput').val(function(index,value){
return value+ id +',1;';
});
addTotal($('#price'+id).text());
}
imageSync();
}else{
//This is where my question refers to.
$('a#'+id+'.courseDel').click();
}
}
当有人选中该复选框时,包含一些数据和链接的跨度将添加到页面中。新链接连接到一个不同的函数,
$('.courseDel').live('click', del_course);
del_course 做了很多事情,就像 add_course 一样。
正如您在 add_course 函数中所看到的。我检查该复选框是否已被选中,并且仅在已选中的情况下才执行操作。
这是 del_course:
function del_course(){
var id = $(this).attr('id');
$('#cn'+id).remove();
$('#courseInput').val(function(index,value){
return value.replace(id+',1;',"");
});
subtractTotal($('#price'+id).text());
imageSync();
}
我想让 add_course 函数的 else 部分触发 del_course 以获取选中复选框时添加的相应链接。这不起作用。我可能把事情搞得太复杂了。
这是复选框的 html(其中一个示例):
<input type="checkbox" class="courseAdd" name="courseAdd" id="204"/>
这是当有人单击复选框时添加的链接的 html:
<span id="cn204"><a href="#" class="courseDel" id="204">X</a> Course Title<br/></span>
当有人单击链接时效果很好,但我如何以编程方式触发它?
I am trying to do something tricky (at least to me) in jquery. I have a checkbox that is bound to a function called add_course which looks like this:
function add_course(){
var id = $(this).attr('id');
if ($(this).is(':checked')){
if($('#courseInput').val().search(id) < 0){
$('#CourseSummary').append('<span id="cn'+id+'"><a href="#" class="courseDel" id="'+id+'">X</a> '+$('#course'+id+' a').html()+'<br/></span>');
$('#courseInput').val(function(index,value){
return value+ id +',1;';
});
addTotal($('#price'+id).text());
}
imageSync();
}else{
//This is where my question refers to.
$('a#'+id+'.courseDel').click();
}
}
When someone checks the checkbox, a span with some data and a link are added to the page. The new link is connected to a different function with
$('.courseDel').live('click', del_course);
del_course does a whole bunch of stuff, just like add_course.
As you can see in the add_course function. I check whether the checkbox has been checked and only do stuff if it has been.
here is del_course:
function del_course(){
var id = $(this).attr('id');
$('#cn'+id).remove();
$('#courseInput').val(function(index,value){
return value.replace(id+',1;',"");
});
subtractTotal($('#price'+id).text());
imageSync();
}
I would like to have the else part of my add_course function trigger the del_course for the corresponding link that got added when the checkbox was checked. This isn't working. I've probably over complicated something.
here is the html for the checkbox (an example of one of them):
<input type="checkbox" class="courseAdd" name="courseAdd" id="204"/>
here is the html for the link that gets added when someone clicks a checkbox:
<span id="cn204"><a href="#" class="courseDel" id="204">X</a> Course Title<br/></span>
It works great when someone clicks the link, but how do i trigger it programatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您拥有创建的元素的 ID,因此只需引用该 ID 并使用
trigger()< /code>
方法:
此外,仅是数字的 ID 是不正确的:
Since you have the ID of the element that you created, simply refernce the ID and use the
trigger()
method:Also, an ID that is just a number is incorrect:
使用 jquery 的 trigger() 函数。
Use jquery's trigger() function.
从长远来看,稍微重新组织代码可能会有所帮助,以便将 DOM 事件处理与应用程序逻辑分离。
In the long term, it might help to reorganize your code a bit so that you decouple the DOM event-handling from the application logic.