问题 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>

有人点击链接时效果很好,但我如何以编程方式触发它?


7693
2017-10-21 17:53


起源

这里要注意的2个项目:首先, id 应该是独一无二的,不要像你拥有它一样分享 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 (".").  w3.org/TR/html4/types.html   也, stackoverflow.com/questions/70579/... - josh.trow
在重读了这个问题之后,我不确定我理解 - 你遇到的问题是什么://这是我的问题所指的问题。 $( 'A#' + ID + 'courseDel')点击(); - jbabey


答案:


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

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

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

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


9
2017-10-21 17:59



更不用说他有多个具有相同ID值的元素 - josh.trow
问题在于我的命名约定。 - lovefaithswing


使用jquery 触发() 功能。

$('.courseDel').trigger('click');

1
2017-10-21 17:56



这将触发与该类的所有锚点的click事件 courseDel。我相信OP想要触发刚刚添加的锚点击事件。 - James Hill


从长远来看,可能有助于重新组织您的代码,以便您将DOM事件处理与应用程序逻辑分离。

//Functions to add and remove stuff (these are *not* event handlers)
//Since they now receive explicit parameters
// you have full power over when to add and remove stuff and functions
// can very easily call eachother if needed.

//You can have any kind of state you might need, without having to 
//trick the dom into storing it for you:
var currentlySelectedCourse = null;

function add_course(id){
    //its now easy to access global state like the 
    //currently open course
    //and it is now easy to call other functions like del_course
}

function del_course(id){
}

//event handling can now become just a simple layer...

$('.courseDel').live('click', function(){
     var id = //get the id or what parameters you need
     del_course(id); //pass it to the backend function
}); 

0
2017-10-21 18:06