1

我真的很困惑,我有一个包含很多脚本的 js 文件,它们都正常工作,我只是添加了新行:

$('.RemoveE').click(function () {
    alert('yap');
});

但这不起作用,我用firebug对其进行了测试,一切正常,我也尝试了另一个也不起作用的浏览器,我必须提到我="ReomoveE button red"通过脚本添加了带有CSS类的新按钮,这是我的js的一部分,包括新行并添加按钮:

// <reference path="../jquery-1.7.1.js" />
$(document).ready(function() {

    $('.RemoveE').click(function() {
        alert('yap');
    });

    //Add new Addable div
    $('.AddNewE').click(function() {

        var Target = $('.Addable:first');
        var TargetId = $(Target).attr('id');
        var Count = $('.Addable#' + TargetId).size();
        var CloneTarget = $(Target).clone();
        CloneTarget.find('input').val('');
        CloneTarget.insertAfter('.Addable:last');
        var TargetName = $(Target).find('input').attr('name');
        var CloneName = TargetName + '[1]';
        TargetName = TargetName + '[0]';
        $(Target).find('input').attr('name', TargetName);
        $(Target).find('span[class*="field-validation"]').attr('data-valmsg-for', TargetName);
        $(CloneTarget).find('input').attr('name', CloneName);
        $(CloneTarget).append($('<input type="button" class="RemoveE button red" value="remove" />'));

        (function($) {
            $.fn.updateValidation = function() {
                var form = this.closest("form").removeData("validator").removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse(form);
                return this;
            };
        })(jQuery);

        $(Target).updateValidation();
        $(CloneTarget).updateValidation();
    });
    ...
});​

所以你怎么看?问题出在哪里,为什么所有功能都正常工作,但这个新功能没有?

4

3 回答 3

2

您在元素存在之前添加了处理程序:

$('.RemoveE').click(function() {
    alert('yap');
});

//then later

$(CloneTarget).append($('<input type="button" class="RemoveE button red" value="remove" />'));

尝试:

在创建时添加处理程序:

var newInput = $('<input type="button" class="RemoveE button red" value="remove" />')

newInput.click(function(){
    alert('yap');
});

$(CloneTarget).append(newInput)

或委托on()

$(parent_of_RemoveE).on('click','.RemoveE',function() {
    alert('yap');
});
于 2012-05-09T05:24:07.720 回答
1

您需要使用.on()or.delegate()这样它就可以收听您动态创建的任何新按钮

在此处查看示例代码

HTML

​<code><div id='test'></div>​​​​​​​​​​​​​​​​​​</code>

查询

​$(document).ready(function() { 
    $('#test').on('click','.removeE', function() {
     alert('yap');
    });

    $('#test').append('<input type="button" class="removeE" value="test"/>');
});​

你可以在这里看到这个例子

于 2012-05-09T05:28:05.647 回答
1

@Joseph基本上说了什么。我评论了您的代码位以指出一些值得注意的事情。

$(document).ready(function () {

    $('.RemoveE').click(function () {
        alert('yap');
    });

    //Add new Addable div
    $('.AddNewE').click(function () {

        var Target = $('.Addable:first');
        var TargetId = $(Target).attr('id'); // Double wrapping in jQuery

         // ID are unique, selector makes little sense,
         // and `length` is preferable to `size()`
        var Count = $('.Addable#' + TargetId).size();
        var CloneTarget = $(Target).clone(); // Double wrapping in jQuery
        CloneTarget.find('input').val('');
        CloneTarget.insertAfter('.Addable:last');
        var TargetName = $(Target).find('input').attr('name'); // Double wrapping in jQuery

        // This seems a bit verbose...
        var CloneName = TargetName + '[1]';
        TargetName = TargetName + '[0]';
        $(Target).find('input').attr('name', TargetName); // Double wrapping in jQuery
        $(Target).find('span[class*="field-validation"]').attr('data-valmsg-for', TargetName); // Double wrapping in jQuery
        $(CloneTarget).find('input').attr('name', CloneName); // Double wrapping in jQuery
        $(CloneTarget).append($('<input type="button" class="RemoveE button red" value="remove" />')); // Double wrapping in jQuery
        // end verbosity

        (function ($) {
            $.fn.updateValidation = function () {
                // Don't need to chain `removeData()` twice,
                // as of jQuery 1.7 you can pass a list
                var form = this.closest("form").removeData("validator").removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse(form);
                return this;
            };
        })(jQuery);

        $(Target).updateValidation(); // Double wrapping in jQuery
        $(CloneTarget).updateValidation(); // Double wrapping in jQuery
    });

});
于 2012-05-09T05:33:41.977 回答