简体   繁体   中英

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 복사하기. and makes checkbox.

and then if user click id="print2" button "복사하기" it doesn't work. 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. You need to delegate the click to a parent, typically the document .. See more

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 :

$(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();
    });


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM