[英]second js function doesn't work
if user click <li><a id="print" href="">출력 하기</a></li>
it's id ="print" so it makes id="print2" button, title is 복사하기.如果用户点击
<li><a id="print" href="">출력 하기</a></li>
它是 id ="print" 所以它使 id="print2" 按钮,标题是 복사하기。 and makes checkbox.并制作复选框。
and then if user click id="print2" button "복사하기" it doesn't work.然后如果用户点击 id="print2" 按钮 "복사하기" 它不起作用。 there is no reaction.
没有反应。
what do i miss?我想念什么?
$(document).ready(function(){
$("#print").unbind('click');
$("#print").on('click', function(ev){
$('#main').prepend('<center><button class="btn btn-primary btn-lg" id="print2">복사하기</button></center>')
$('.post').prepend('<input type="checkbox" />');
ev.preventDefault();
});
$("#print2").on('click', function(){
var images ='';
$('li').each(function(){
var thisCheckFlag=$(this).children('input[type="checkbox"]').is(':checked');
if(thisCheckFlag){
images+='<img src ="'+$(this).find('img').attr('src')+'">';
}
});
if(images){
var myWindow=window.open('','printWindow','width=800,height=800');
myWindow.document.write(
'<html><head><title>Print</title></head><body>'
+images+'</body></html>'
);
myWindow.focus();
myWindow.print();
}
else alert('먼저 선택하세요.');
});
});
Your #print2
is not defined when you are attaching the click to it.当您将点击附加到它时,您的
#print2
未定义。 You need to delegate the click to a parent, typically the document
.. See more您需要将点击委托给父级,通常是
document
..查看更多
So your function will become:所以你的函数将变成:
$(document).on('click', '#print2', function(){
//rest of code.
});
The problem here is that the event will not bind with the dynamically created elements,这里的问题是事件不会与动态创建的元素绑定,
So whatever you add a new button you need to add the event
associated to it so :因此,无论您添加一个新按钮,您都需要添加与之关联的
event
:
$(document).ready(function() {
$("#print").unbind('click');
$("#print").on('click', function(ev) {
$('#main').prepend('<center><button class="btn btn-primary btn-lg" id="print2">복사하기</button></center>')
$('.post').prepend('<input type="checkbox" />');
$("#print2").on('click', function() {
var images = '';
$('li').each(function() {
var thisCheckFlag = $(this).children('input[type="checkbox"]').is(':checked');
if (thisCheckFlag) {
images += '<img src ="' + $(this).find('img').attr('src') + '">';
}
});
if (images) {
var myWindow = window.open('', 'printWindow', 'width=800,height=800');
myWindow.document.write(
'<html><head><title>Print</title></head><body>' + images + '</body></html>'
);
myWindow.focus();
myWindow.print();
} else alert('먼저 선택하세요.');
});
ev.preventDefault();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.