📄 ch04_02.htm
字号:
enter a line of data (see <a href="ch04_02.htm#ch04-59247">Figure 4-2</a>). Text fieldsare the default input type; if you omit the TYPE attribute, you willget a text field. The HTML for a text field looks like this:</p><blockquote><pre class="code"><INPUT TYPE="text" NAME="quantity" VALUE="1" SIZE="3" MAXLENGTH="3"></pre></blockquote><a name="ch04-59247" /><div class="figure"><img width="443" src="figs/cgi2.0402.gif" height="205" alt="Figure 4-2" /></div><h4 class="objtitle">Figure 4-2. Text and password fields</h4><p>Here are the <a name="INDEX-799" /> <a name="INDEX-800" />attributes that apply to text fields:</p><dl><dt><b>VALUE</b></dt><dd><p>The <a name="INDEX-801" />VALUE of text fields is the defaulttext displayed in the text field when the form is initially presentedto the user. It defaults to an empty string. The user can edit thevalue of text fields; updates change what is displayed as well as thevalue passed when the form is submitted.</p></dd><dt><b>SIZE</b></dt><dd><p>The <a name="INDEX-802" />SIZE attribute specifies the width ofthe text field displayed. It roughly corresponds to the number ofcharacters the field can hold, but this is generally only accurate ifthe element is surrounded by <TT> or <PRE> tags, whichindicate that a monospace font should be used. Unfortunately,Netscape and Internet Explorer render the width of fields verydifferently when monospaced fonts are not used, so certainly testyour form with both browsers. The default SIZE for text fields is 20.</p></dd><dt><b>MAXLENGTH</b></dt><dd><p>The <a name="INDEX-803" />MAXLENGTH attribute specifies themaximum number of characters that a text field can hold. Browsersgenerally do not allow users to enter more characters than this.Because the size of text fields can vary with variable-width fonts,it is possible to set MAXLENGTH and SIZE to the same value and yethave a field that appears too large or too small for that number ofcharacters. A text field can have a MAXLENGTH set to more charactersthan its SIZE can display. By default, there is no specified limit onthe size of text fields.</p></dd><dt><b>onFocus, onBlur, onChange</b></dt><dd><p>The <a name="INDEX-804" /><a name="INDEX-805" /><a name="INDEX-806" />JavaScript handlers are onFocus,onBlur, and onChange, which are called when the text field has focus(the input cursor is in the field), loses focus (the cursor moves outof the field), and when the value of the field changes, respectively.</p></dd></dl></div><a name="ch04-5-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.2. Password fields</h3><p>A <a name="INDEX-807" /><a name="INDEX-808" />password field is similar to a textfield, except that instead of displaying the true value of the field,the browser represents each character with an asterisk or bullet(refer back to <a href="ch04_02.htm#ch04-59247">Figure 4-2</a>):</p><blockquote><pre class="code"><INPUT TYPE="password" NAME="set_password" VALUE="old_password" SIZE="8" MAXLENGTH="8"></pre></blockquote><p>This field does not provide any true security; it simply providesbasic protection against someone looking over the shoulder of theuser. The value is <em class="emphasis">not</em> encrypted when it istransferred to the web server, which means that passwords aredisplayed as part of the <a name="INDEX-809" /><a name="INDEX-810" />query string for GET requests.</p><p>All the <a name="INDEX-811" /><a name="INDEX-812" />attributes that apply to textfields also apply to password fields.</p></div><a name="ch04-6-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.3. Hidden fields</h3><p><a name="INDEX-813" />Hidden fieldsare not visible to the user. They are generally used only with formswhich are themselves generated by a CGI script and are useful forpassing information between a series of forms:</p><blockquote><pre class="code"><INPUT TYPE="hidden" NAME="username" VALUE="msmith"></pre></blockquote><p>Like password fields, hidden fields provide no security. Users canview the name and value of hidden fields by viewing the HTML sourcein their browsers.</p><p>We'll discuss hidden fields in much more detail in ourdiscussion of maintaining state in <a href="ch11_01.htm">Chapter 11, "Maintaining State"</a>.</p><p>Hidden fields only use <a name="INDEX-814" /><a name="INDEX-815" /><a name="INDEX-816" /> <a name="INDEX-817" />NAME and VALUE attributes.</p></div><a name="ch04-7-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.4. Checkboxes</h3><p><a name="INDEX-818" />Checkboxes areuseful when users simply need to indicate whether they desire anoption. See <a href="ch04_02.htm#ch04-88757">Figure 4-3</a>.</p><a name="ch04-88757" /><div class="figure"><img width="452" src="figs/cgi2.0403.gif" height="325" alt="Figure 4-3" /></div><h4 class="objtitle">Figure 4-3. Checkboxes</h4><p>The user can <a name="INDEX-819" />toggle between two states on acheckbox: checked or unchecked. The tag looks like this:</p><blockquote><pre class="code"><INPUT TYPE="checkbox" NAME="toppings" VALUE="lettuce" CHECKED></pre></blockquote><p>In this example, if the user selects the checkbox, then"toppings" returns a value of "lettuce". Ifthe checkbox is not selected, neither the <a name="INDEX-820" /><a name="INDEX-821" /><a name="INDEX-822" />namenor the value is returned for the checkbox.</p><p>It is possible to have multiple checkboxes use the same name. Infact, this is not uncommon. The most typical situation in which youmight do this is if you have a dynamic list of related options andthe user could choose a similar action for all of them. For example,you may wish to list multiple options this way:</p><blockquote><pre class="code"><INPUT TYPE="checkbox" NAME="lettuce"> Lettuce<BR><INPUT TYPE="checkbox" NAME="tomato"> Tomato<BR><INPUT TYPE="checkbox" NAME="onion"> Onion<BR></pre></blockquote><p>If, however, the CGI script does not need to know the name of each ofthe options in order to perform its task, you may wish to do thisinstead:</p><blockquote><pre class="code"><INPUT TYPE="checkbox" NAME="toppings" VALUE="lettuce"> Lettuce<BR><INPUT TYPE="checkbox" NAME="toppings" VALUE="tomato"> Tomato<BR><INPUT TYPE="checkbox" NAME="toppings" VALUE="onion"> Onion<BR></pre></blockquote><p>If someone selects "lettuce" and "tomato" butnot "onion", then the browser will encode this as<tt class="literal">toppings=lettuce&toppings=tomato</tt>. The CGIscript can process these multiple toppings, and you may not need toupdate the CGI script if you later add items to the list. Attributesfor checkboxes include:</p><dl><dt><b>VALUE</b></dt><dd><p>The VALUE attribute is the value included in the request if thecheckbox is checked. If a VALUE attribute is not specified, thecheckbox will return "ON" as its value. If the checkboxis not checked, then neither its name nor value will be sent.</p></dd><dt><b>CHECKED</b></dt><dd><p>The <a name="INDEX-823" />CHECKED attribute indicates that thecheckbox should be selected by default. Omitting this attributecauses the checkbox to be unselected by default.</p></dd><dt><b>onCheck</b></dt><dd><p>Checkboxes also take the <a name="INDEX-824" /> <a name="INDEX-825" />onCheck attribute, whichindicates the JavaScript code that should be executed when thecheckbox is selected.</p></dd></dl></div><a name="ch04-8-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.5. Radio buttons</h3><p><a name="INDEX-826" /><a name="INDEX-827" />Radio buttons are very similar tocheckboxes except that any group of radio buttons that share the samename are exclusive: only one of them may be selected. See <a href="ch04_02.htm#ch04-92305">Figure 4-4</a>.</p><a name="ch04-92305" /><div class="figure"><img width="449" src="figs/cgi2.0404.gif" height="342" alt="Figure 4-4" /></div><h4 class="objtitle">Figure 4-4. Radio buttons</h4><p>The tag is used just like a checkbox:</p><blockquote><pre class="code"><INPUT TYPE="radio" NAME="bread" VALUE="wheat" CHECKED> Wheat<BR><INPUT TYPE="radio" NAME="bread" VALUE="white"> White<BR><INPUT TYPE="radio" NAME="bread" VALUE="rye"> Rye<BR></pre></blockquote><p>In this example, "wheat" is selected by default.Selecting "white" or "rye" will cause"wheat" to be unselected.</p><p>Although you may omit the <a name="INDEX-828" />VALUE attribute with checkboxes, doingso with radio buttons is meaningless since the CGI script will not beable to differentiate between different radio buttons if they allreturn "ON".</p><p>Using the <a name="INDEX-829" /><a name="INDEX-830" />CHECKED attribute with multiple radiobuttons with the same name is not valid. Browsers will generallyrender both as selected, but they will be unselected as soon as theuser selects a different option and the user will be unable to returnthe form to this initial state (unless it has a reset button ofcourse).</p><p>Radio buttons use the same attributes as checkboxes.</p></div><a name="ch04-9-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.6. Submit buttons</h3><p>A <a name="INDEX-831" /><a name="INDEX-832" /> <a name="INDEX-833" /><a name="INDEX-834" />submit button does just what the nameimplies. It submits the contents of the form (see <a href="ch04_02.htm#ch04-39427">Figure 4-5</a>). When the user clicks on a submit button, thebrowser runs any associated JavaScript <em class="emphasis">onSubmit</em>handler, formats an HTTP request according to the form method andform encoding type, then sends this request to the URL specified bythe form action. The result is then displayed as a new web page.</p><a name="ch04-39427" /><div class="figure"><img width="448" src="figs/cgi2.0405.gif" height="196" alt="Figure 4-5" /></div><h4 class="objtitle">Figure 4-5. Submit buttons</h4><p>The HTML for a submit button looks like this:</p><blockquote><pre class="code"><INPUT TYPE="submit" NAME="submit_button" VALUE="Submit the Form"></pre></blockquote><p>Virtually all forms have a submit button, and you can have multiplesubmit buttons on one form:</p><blockquote><pre class="code"><INPUT TYPE="submit" NAME="option" VALUE="Option 1"><INPUT TYPE="submit" NAME="option" VALUE="Option 2"></pre></blockquote><p>Only the name and value of the submit button clicked is included inthe form submission. Here are the attributes it supports:</p><dl><dt><b>VALUE</b></dt><dd><p>The <a name="INDEX-835" />VALUE attribute for submit buttonsspecifies the text that should be displayed on the button as well asthe value supplied for this element when the form is submitted. Ifthe value is omitted, browsers supply a default label -- generally"Submit" -- and refrain from submitting a name andvalue for this element.</p></dd><dt><b>onClick</b></dt><dd><p>Submit buttons may have an <a name="INDEX-836" /><a name="INDEX-837" />onClick JavaScript handler, whichspecifies the code to execute if the user clicks the button.Returning a false value from this code cancels the submit operation.</p></dd></dl></div><a name="ch04-10-fm2xml" /><div class="sect3"><h3 class="sect3">4.2.3.7. Reset buttons</h3><p>A <a name="INDEX-838" /><a name="INDEX-839" /><a name="INDEX-840" />reset button allows users to reset thevalue of all the fields in a form to their default values. From theuser's perspective, it generally accomplishes the same thing asreloading the form but is much faster and more convenient. Becausethe browser accomplishes this event without consulting the webserver, CGI scripts never respond to it. The HTML tag looks likethis:</p><blockquote><pre class="code"><INPUT TYPE="reset" VALUE="Reset the form fields"></pre></blockquote><p>You may have multiple<a name="INDEX-841" /> <a name="INDEX-842" /><a name="INDEX-843" /> <a name="INDEX-844" /><a name="INDEX-845" />reset buttons on the same form,although this would almost certainly be redundant.</p><dl><dt><b>NAME</b></dt><dd><p>You may specify a NAME for reset buttons, but neither the name northe value is ever passed to a CGI script. Thus, the name is onlyuseful to JavaScript code.</p></dd><dt><b>VALUE</b></dt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -