Home >Web Front-end >Layui Tutorial >How to implement layui printing form
To use the layui framework to print a form, you need to perform the following steps in sequence: introduce the layui file, create a print button, listen to button events, obtain the form content, and call the layui print function. For example, use jQuery to get the form content and print it: $('#form').html() passed to print({title: 'Form Print', content: content}).
Implementation of layui printing form
Question: How to use the layui framework to print a form?
Answer:
To use the layui framework to print a form, the following steps are required:
Step 1: Import the layui file
<code class="html"><script src="layui/layui.js"></script></code>
Step 2: Create a button
Create a button to trigger the print function.
<code class="html"><button id="printBtn">打印表单</button></code>
Step 3: Listen to button events
Use layui’s on()
method to listen to button click events.
<code class="javascript">layui.use(['layer', 'print'], function () { var layer = layui.layer; var print = layui.print; layui.on('click', '#printBtn', function () { // ... }); });</code>
Step 4: Get the form content
In the listener function, use the html()
method of jQuery or layui to get the form content.
For example, using jQuery:
<code class="javascript">var content = $('#form').html();</code>
Step 5: Call the layui print function
Use the print()
method to print the form content.
<code class="javascript">print({ title: '表单打印', // 打印标题 content: content, // 打印内容 });</code>
Sample code:
<code class="html"><!DOCTYPE html> <html> <head> <script src="layui/layui.js"></script> </head> <body> <form id="form"> <input type="text" value="姓名"> <input type="text" value="年龄"> </form> <button id="printBtn">打印表单</button> <script> layui.use(['layer', 'print'], function () { var layer = layui.layer; var print = layui.print; layui.on('click', '#printBtn', function () { var content = $('#form').html(); print({ title: '表单打印', content: content }); }); }); </script> </body> </html></code>
The above is the detailed content of How to implement layui printing form. For more information, please follow other related articles on the PHP Chinese website!