我真的很困惑,我有一个带有很多脚本的js文件,所有这些都正常工作,我只是添加新行:
$('.RemoveE').click(function () {
alert('yap');
});
但是没有用,我用firebug测试它,一切都没问题,我也尝试了其他浏览器也没用,我必须提一下我用脚本添加CSS类="ReomoveE button red"
的新按钮,这是我的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();
});
...
});
那你觉得怎么样?问题出在哪里,为什么所有功能都能正常工作,但这个新功能没有?
答案 0 :(得分: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');
});
答案 1 :(得分:1)
您需要使用.on()
或.delegate()
,以便它可以收听您动态创建的任何新按钮
请参阅此处的示例代码
HTML
<div id='test'></div>
JQUERY
$(document).ready(function() {
$('#test').on('click','.removeE', function() {
alert('yap');
});
$('#test').append('<input type="button" class="removeE" value="test"/>');
});
您可以看到示例here
答案 2 :(得分: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
});
});