javascript当中document.forms的用法

马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
例 2.1(FormActionSubmitIEFF.html)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <TITLE></TITLE>
    <SCRIPT LANGUAGE="JavaScript">
        /*
         JavaScript syntax: - myObject.onclick = aHandler
         HTML syntax: <A onClick="..."> <AREA onClick="..."> <HTMLTag onClick="..."> <INPUT onClick="...">

         Form object (Object/HTML)
         An object representing an HTML <FORM> tag.
         */
        function clickH(actUrl)
        {
            /*Document.forms[] (Collection)
             An array containing a list of all the forms in the document.*/
            var fm = document.forms[0];
            /*Form.action (Property)
             A property that contains the submit action method for the <FORM>.
             JavaScript syntax: - myForm.action
             HTML syntax: <FORM ACTION="...">
             */
            fm.action = actUrl;
            /*Form.submit() (Method)
             Invoke a form submit to the server.*/
            fm.submit();
        }
    </SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=POST>
    <input name="name" type="text"/><br>
    <input type="button" value="提交到第一个地址" onClick="clickH('1.htm?k=b')"/>
    <input type="button" value="提交到第二个地址" onClick="clickH('2.htm')"/>
</FORM>
</BODY>
</HTML>





例 2.2(FormObjectReferenceIEFF.html)



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
    function test()
    {      
        var txt1 = document.forms[0].elements[0].value
        var txt2 = document.regForm.username.value
        var psw1 = document.forms[0].elements[1].value
        var psw2 = regForm.psw.value;

        with(document)
        {
            write("document.forms[0].elements[0].value-->"+txt1+"<br>")
            write("document.regForm.username.value-->"+txt2+"<br>")
            write("document.forms[0].elements[1].value-->"+psw1+"<br>")
            write("regForm.psw.value-->"+psw2+"<br>")
        }
    }
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION="" name="regForm">
    <INPUT TYPE="text" NAME="username"><BR>
    <INPUT TYPE="password" name="psw"><BR>
    <TEXTAREA NAME="" ROWS="" COLS=""></TEXTAREA>
    <INPUT TYPE="button" value="click" onclick="test()">
</FORM>
</BODY>
</HTML>