Printing an HTML form using jQuery

坚强是说给别人听的谎言 提交于 2019-12-13 10:53:26

问题


I am having an HTML form, and I need to print(HardCopy) particular area..

The problem is I am unable to get the form Text1 and Text2 values on the. Since I need that HTML form to be filled with some value, while clicking print it should print as hardcopy.

My Code

        <script type="text/javascript">
            $(document).ready(function(){
                $("#print").click(function(){
                   alert($('#click').html());
                   // It shows only form elements; I need a form typed element too. How?
                   window.print();
                });
            });
        </script>
    </head>
    <body>
        <form id="click">
            Text1: <input id="l" type="text" />
            Text2: <input id="m" type="text"/>
            <input id="print" type="button" value="Print-Me"/>
        </form>
    </body>
</html>

回答1:


From what I can understand, you want the text that is typed in the text1 and text2 input fields. You can do so with the .val() function, like this:

$("#print").click(function(){
    alert($('#l').val() + '\n' + $('#m').val());
    window.print();
});



回答2:


Use the Print Element Plugin in the jQuery library:

$("#print").click(function(){
    printElem({ printMode: 'popup' });
});
function printElem(options){
    $('#click').printElement(options);
}


来源:https://stackoverflow.com/questions/5457254/printing-an-html-form-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!