[英]How to add event listener to an element when it created with jquery
I'm trying to create and add event listener to a button in jquery. 我正在尝试创建事件监听器并将其添加到jquery中的按钮。 I'm able to achieve this, but What is my problem is that when I click any of the button in the form, this event is triggering.
我能够实现这一点,但我的问题是,当我单击表单中的任何按钮时,此事件就会触发。
here is my code: 这是我的代码:
$('#submitPh').on('click',function(e){
e.preventDefault();
secCheck($(this).attr('id'));
});
function secCheck(invoker) {
console.log('called');
var id = 'secModal', title = '<h3>Security Verification</h3>',
body = $('<form/>').attr({'method': 'post', 'action': '', 'id': 'secCheck'}).append($('<div/>').addClass('form-group')
.append($('<div/>').addClass('controls')
.append($('<label/>').attr({'for': 'verPass'}).addClass('control-label').text('Please enter your password'), $('<input/>').attr({'type': 'password', 'id': 'verPass', 'name': 'verPass', 'value': '', 'placeholder': 'Enter your password'})
.addClass('form-control'))), $('<div/>').addClass('form-group').append($('<div/>').addClass('col-lg-10 warning-mes')),$('<div/>').addClass('form-group').append($('<button/>').click(confCheck()).attr({'type': 'button', 'id': 'secCheckSubmit'}).addClass('btn btn-default').text('Submit')));
createModal(id, title, body, invoker);
}
function createModal(id, title, body, invoker) {
var modal = $('<div/>').attr('id', id).addClass('modal fade')
.append($('<div/>').addClass('modal-dialog')
.append($('<div/>').addClass('modal-content')
.append($('<div/>').addClass('modal-header')
.append($('<button/>').attr({'data-dismiss': 'modal', 'area-hidden': true}).addClass('close').html('×'), title), $('<div/>').addClass('modal-body').html(body), $('<div/>').addClass('modal-footer')
.append($('<button/>').attr({'type': 'button', 'data-dismiss': 'modal'}).addClass('btn btn-default').text('Close')))));
if (!$('div#' + id).length) {
$('body').append(modal);
}
showModal('#' + id, invoker);
}
function showModal(sId, invoker) {
var $this = invoker;
var id = sId;
var $target = $(sId || (id && id.replace(/.*(?=#[^\s]+$)/, ''))); //strip for ie7
var option = $target.data('modal') ? 'toggle' : $.extend({remote: !/#/.test(id) && id}, $target.data());
this.modal = $target
.modal(option, this)
.one('hide', function() {
$this.is(':visible') && $this.focus();
});
}
function confCheck() {
alert();
}
This is how I did. 这就是我的方式。 But this was not producing the expected result as I stated above.
但是,如上所述,这并没有产生预期的结果。
Please anyone help me to solve this problem... 请有人帮我解决这个问题......
Edit 编辑
You can use .appendTo() in this context 您可以在此上下文中使用.appendTo()
Try, 尝试,
$('<button/>').appendTo('#button').click(sp.confCheck(func,data));
it should be 它应该是
$('<button/>').click(confCheck)
You need to pass a function reference as the parameter to click()
, don't invloke it ans sent the result 您需要将函数引用作为参数传递给
click()
,不要将其调用并发送结果
better you should give a class name for the newly created button and provide deligate for event listening 更好的是,您应该为新创建的按钮提供类名,并为事件监听提供详细信息
$('#button').append($('<button/>', { class: "test" }));
$("body").on("click", ".test", function () { });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.