2009年1月6日

form 特徵

特徵 | 方法
form 的特徵

文件中的 FORM 元素可以用 document.forms 存取。

elements

存放 FORM 元素下,所有的輸入控制元素之清單陣列。可以用整數值或 id 名稱,讀取此陣列。

  • 程式用法:

    <form id='fm1' name='form1' accept-charset='big5 utf8'
      action='hello.php' enctype="multipart/form-data"
      method='post' target='_blank'>
    <input id='p1' size=12 value='小遊戲'>
    <button id='p2'>好玩遊戲</button>
    </form><p>
    <script type='text/javascript'>
    for( var i=0; i < document.forms['fm1'].elements.length; i++)
    {
      var po=document.forms['fm1'].elements[i];
      document.write( po.nodeName
        +' : '+ po.id +' : '+ 
        ((po.nodeName=='BUTTON') ? po.innerHTML : po.value) +'<br />');
    }
    document.write('<p>'+ 
      document.forms['fm1'].elements['p1'].value +' / ');
    document.write( 
      document.forms['fm1'].elements['p2'].innerHTML );
    </script>

  • 執行結果:

    INPUT : p1 : 小遊戲
    BUTTON : p2 : 好玩遊戲

    小遊戲 / 好玩遊戲

acceptCharset

存放當下 FORM 元素支援的文字組清單。必須有設 ACCEPT-CHARSET 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.getElementById('fm1').acceptCharset );
    </script>

  • 執行結果:

    big5 utf8

action

讀取或設定 FORM 元素的行動。必須有設 ACTION 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.forms['fm1'].action );
    </script>

  • 執行結果:

    http://ant4js.blogspot.com/2009/01/hello.php

  • IE, Firefox 的輸出是”http://ant4js.blogspot.com/hello.php”;Safari, Chrome 的輸出是”hello.php”。
encoding

讀取或設定 FORM 元素的內容編碼型態,與 enctype 相同。

enctype

讀取或設定 FORM 元素的內容編碼型態。必須有設 ENCTYPE 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.forms['fm1'].enctype +'<br />');
    document.write( document.forms['fm1'].encoding );
    </script>

  • 執行結果:

    multipart/form-data
    multipart/form-data

length

存放 FORM 元素下,輸入控制元素之個數。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.forms['fm1'].length );
    </script>

  • 執行結果:

    2

method

讀取或設定 FORM 元素上傳的方法。必須有設 METHOD 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.forms['fm1'].method );
    </script>

  • 執行結果:

    post

name

存放當下 FORM 元素的名稱字串。必須有設 NAME 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.getElementById('fm1').name );
    </script>

  • 執行結果:

    form1

target

讀取或設定 FORM 元素,行動的展現目標。必須有設 TARGET 屬性。

  • 程式用法:

    <script type='text/javascript'>
    document.write( document.forms['fm1'].target );
    </script>

  • 執行結果:

    _blank


©2008-2009 by ant2legs, All Rights Reserved. ant2legs 擁有其製作的文章,圖片與程式的著作權,所有權利保留。