javascript参考 表单元素的属性和方法 一
Lets users input text and make choices from Form elements such as checkboxes, radio buttons, and selection lists. You can also use a form to post data to a server.
属性
action
A string specifying a destination URL for form data that is submitted
属性源 Form
描述
The action property is a reflection of the ACTION attribute of the FORM tag. Each section of a URL contains different information. See Location for a描述 of the URL components.
示例
The following example sets the action property of the musicForm form to the value of the variable urlName:
document.musicForm.action=urlName
elements
An array of objects corresponding to form elements (such as checkbox, radio, and Text objects) in source order.
属性源 Form
只读
描述
You can refer to a form's elements in your code by using the elements array. This array contains an entry for each object (Button, Checkbox, FileUpload, Hidden, Password, Radio, Reset, Select, Submit, Text, or Textarea object) in a form in source order. Each radio button in a Radio object appears as a separate element in the elements array. For example, if a form called myForm has a text field and two checkboxes, you can refer to these elements myForm.elements[0], myForm.elements[1], and myForm.elements[2].
Although you can also refer to a form's elements by using the element's name (from the NAME attribute), the elements array provides a way to refer to Form objects programmatically without using their names. For example, if the first object on the userInfo form is the userName Text object, you can evaluate it in either of the following ways:
userInfo.userName.value
userInfo.elements[0].value The value of each element in the elements array is the full HTML statement for the object.
encoding
A string specifying the MIME encoding of the form.
属性源 Form
实现版本 Navigator 2.0
描述
The encoding property initially reflects the ENCTYPE attribute of the FORM tag; however, setting encoding overrides the ENCTYPE attribute.
示例
The following function returns the value of the encoding property of musicForm:
function getEncoding() {
return document.musicForm.encoding
}
length
The number of elements in the form.
属性源 Form
只读
描述
The form.length property tells you how many elements are in the form. You can get the same information using form.elements.length.
method
A string specifying how form field input information is sent to the server.
属性源 Form
描述
The method property is a reflection of the METHOD attribute of the FORM tag. The method property should evaluate to either "get" or "post".
示例
The following function returns the value of the musicForm method property:
function getMethod() {
return document.musicForm.method
}
name
A string specifying the name of the form.
属性源 Form
描述
The name property initially reflects the value of the NAME attribute. Changing the name property overrides this setting.
示例
In the following example, the valueGetter function uses a for loop to iterate over the array of elements on the valueTest form. The msgWindow window displays the names of all the elements on the form:
newWindow=window.open("http://home.netscape.com") function valueGetter() {
var msgWindow=window.open("")
for (var i = 0; i < newWindow.document.valueTest.elements.length; i++) {
msgWindow.document.write(newWindow.document.valueTest.elements[i].name + "<BR>")
}
}
target
A string specifying the name of the window that responses go to after a form has been submitted.
属性源 Form
描述
The target property initially reflects the TARGET attribute of the A, AREA, and FORM tags; however, setting target overrides these attributes.
You can set target using a string, if the string represents a window name. The target property cannot be assigned the value of a JavaScript expression or variable.
示例
The following example specifies that responses to the musicInfo form are displayed in the msgWindow window:
document.musicInfo.target="msgWindow"
方法
handleEvent
调用指定事件的控制句柄。
方法源 Form
语法
handleEvent(event)
参数
event 你想要调用的对象的某一事件控制句柄的名称。
reset
Simulates a mouse click on a reset button for the calling form.
方法源 Form
语法
reset()
参数
无
描述
The reset method restores a form element's default values. A reset button does not need to be defined for the form.
示例
The following example displays a Text object in which the user is to type "CA" or "AZ". The Text object's onChange event handler calls a function that executes the form's reset method if the user provides incorrect input. When the reset method executes, defaults are restored and the form's onReset event handler displays a message.
<SCRIPT>
function verifyInput(textObject) {
if (textObject.value == 'CA' || textObject.value == 'AZ') {
alert('Nice input')
}
else { document.myForm.reset() }
}
</SCRIPT> <FORM NAME="myForm" onReset="alert('Please enter CA or AZ.')">
Enter CA or AZ:
<INPUT TYPE="text" NAME="state" SIZE="2" onChange=verifyInput(this)><P>
</FORM>
submit
Submits a form.
方法源 Form
语法
submit()
参数
无
描述
The submit method submits the specified form. It performs the same action as a submit button.
Use the submit method to send data back to an HTTP server. The submit method returns the data using either "get" or "post," as specified in Form.method.
示例
The following example submits a form called musicChoice:
document.musicChoice.submit() If musicChoice is the first form created, you also can submit it as follows:
document.forms[0].submit() See also the example for Form.
本文主题表单