jquery以编程方式单击新的dom元素 [英] jquery programmatically click on new dom element

查看:84
本文介绍了jquery以编程方式单击新的dom元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jquery中做一些棘手的事情(至少对我来说)。我有一个绑定到名为add_course的函数的复选框,如下所示:

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做了很多东西,就像add_course一样。

del_course does a whole bunch of stuff, just like add_course.

正如您在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.

这里是del_course:

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();
    }

我想让add_course函数的else部分触发del_course选中复选框时添加的相应链接。这不起作用。我可能过于复杂了。

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.

这里是复选框的html(其中一个示例):

here is the html for the checkbox (an example of one of them):

<input type="checkbox" class="courseAdd" name="courseAdd" id="204"/>

这是当有人点击复选框时添加的链接的html:

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?

推荐答案

由于您拥有自己创建的元素的ID,只需引用ID并使用 trigger() 方法:

Since you have the ID of the element that you created, simply refernce the ID and use the trigger() method:

$('#'+id).trigger('click');

此外,只是一个数字的ID不正确:

Also, an ID that is just a number is incorrect:


ID和NAME令牌必须以字母([A-Za-z])开头,可以是
,后跟任意数量的字母,数字([0- 9]),连字符( - ),
下划线(_),冒号(:)和句点(。)。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

这篇关于jquery以编程方式单击新的dom元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆