简体   繁体   English

我需要一种表单,如果选中了表单中的复选框,然后将“提交”按钮打印页面,该表单会将一个类应用于div

[英]I need a form that will apply a class to a div if a checkbox in form is checked then have submit button print the page

The set up is when the user arrives on the page, it will have their information on the page they put in a previous form to review. 设置是当用户到达页面时,它将以先前的形式将其信息放在页面上以供查看。 Also on the page will be a form with 3 options listed with a checkbox by each option, and a button at the bottom of the form that says "Print." 页面上还将显示一个表单,其中包含3个选项,每个选项列出了一个复选框,以及在表单底部显示“打印”的按钮。

The information at the top of the page that the user is reviewing is also applied to 3 letters that are wrapped in divs with display hidden. 用户正在查看的页面顶部的信息也将应用于包裹在div中且显示隐藏的3个字母。 Each checkbox represents one of the hidden letters, let's say letter1, letter2 and letter3. 每个复选框代表一个隐藏的字母,例如letter1,letter2和letter3。

So, I need a form that will have a checkbox for each of the 3 letters, a submit button that prints the entire page upon clicking, and for the form to apply a class to each of the selected letter's div when the print button is clicked, so the print.css will only print the divs that have this class applied to it. 因此,我需要一个具有3个字母中的每个字母的复选框的表单,一个单击时将打印整个页面的提交按钮,以及单击该按钮时该表单将一个类应用于所选字母的div的每个表单,因此print.css将仅打印对其应用了该类的div。 I'd preferably like all this done using javascript or jquery, but really any simple method is welcome. 我希望所有这些都可以使用javascript或jquery完成,但实际上任何简单的方法都可以使用。

Thank you to all that help! 感谢您提供的所有帮助!

<form action="" id="letterPrint">

<input type="checkbox" name="letter1" value="letter1" /> Letter 1

<input type="checkbox" name="letter2" value="letter2" /> Letter 2

<input type="checkbox" name="letter3" value="letter3" /> Letter 3

<input type="submit" value="print" />

</form>

This is my form thus far. 到目前为止,这是我的表格。

First, adding a print style sheet does not need a class to specific divs. 首先,添加打印样式表不需要特定div的类。 The following code makes it so that when your page prints it will take your print css and apply it to the page before it gets sent off to print. 下面的代码可以使您在打印页面时使用打印CSS并将其应用到页面上,然后再发送给打印。

<link rel="stylesheet" type="text/css" href="path/to/screen.css" media="screen" />

<link rel="stylesheet" type="text/css" href="path/to/print.css" media="print"/>

Making it print automatically is more tricky than it would seem. 使它自动打印比看起来要复杂得多。 But, you can use this code to bring up the print box 但是,您可以使用此代码调出打印框

if (window.print) {
    window.print();
}

Sending a page directly to the printer to be printed is not possible because the print information required to send data to the printer is located on the operating system. 无法将页面直接发送到要打印的打印机,因为将数据发送到打印机所需的打印信息位于操作系统上。 JavaScript only interacts with the browser. JavaScript仅与浏览器交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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