亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

form對(duì)象

form對(duì)象

一個(gè)<form>標(biāo)記,就是一個(gè)<form>對(duì)象。


form對(duì)象的屬性

  • name:表單的名稱,主要用來(lái)讓JS來(lái)控制表單。

  • action:表單的數(shù)據(jù)處理程序(PHP文件)。

  • method:表單的提交方式,取值:GET、POST

  • enctype:表單數(shù)據(jù)的編碼方式。


form對(duì)象的方法

  • submit():提交表單,與<input ?type = “submit”?/>功能一樣。

  • reset():重置表單,與重置按鈕功能一樣。


form對(duì)象的事件

  • onsubmit:當(dāng)單擊提交按鈕時(shí)發(fā)生,并數(shù)據(jù)發(fā)往服務(wù)器之前發(fā)生。主要用來(lái)“在表單提交之前進(jìn)行表單驗(yàn)證”。

  • onreset:當(dāng)單擊重置按鈕時(shí)發(fā)生。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script type="text/javascript">
            window.onload = function(){
            //獲取form對(duì)象
            var formObj = document.form1;
            //增加method屬性
            formObj.method = "post";
            //增加action屬性
            formObj.action = "login.php";
        }
        </script>
    </head>
    <body>
        <form name="form1">
            用戶名:<input type="text" name="username" />
            密碼:<input type="password" name="userpwd" />
            <input type="submit" value="提交表單" />
        </form>
    </body>
</html>


獲取表單元素

  • 通過網(wǎng)頁(yè)元素的id來(lái)獲取對(duì)象。document.getElementById(id)

  • 通過HTML標(biāo)簽名來(lái)獲取對(duì)象。parentNode.getElementsByTagName(tagName)

  • 通過name屬性來(lái)獲取表單元素對(duì)象。表單中所有元素的起點(diǎn)都必須是document對(duì)象。

  • 語(yǔ)法:document.formObj.elementObj

  • 訪問方式是三層結(jié)構(gòu)。其中,formObj代表表單對(duì)象,elementObj代表表單元素對(duì)象。

  • 舉例:document.form1.username.value.length


事件返回值

事件的返回值,會(huì)影響對(duì)象的默認(rèn)動(dòng)作。如:<a>標(biāo)記的默認(rèn)動(dòng)作是打開一個(gè)網(wǎng)址。

如果事件返回false,則阻止默認(rèn)動(dòng)作的執(zhí)行;如果事件返回true或空,則默認(rèn)動(dòng)作繼續(xù)執(zhí)行。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
    </head>
    <body>
        <a href="http://ipnx.cn" onclick="return false">PHP中文網(wǎng)</a>
    </body>
</html>

受返回值影響的事件有兩個(gè):onclick、onsubmit。


繼續(xù)學(xué)習(xí)
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> window.onload = function(){ //獲取form對(duì)象 var formObj = document.form1; //增加method屬性 formObj.method = "post"; //增加action屬性 formObj.action = "login.php"; } </script> </head> <body> <form name="form1"> 用戶名:<input type="text" name="username" /> 密碼:<input type="password" name="userpwd" /> <input type="submit" value="提交表單" /> </form> </body> </html>
提交重置代碼