Home > Web Front-end > JS Tutorial > body text

javascript 获取表单元素的几种方法

WBOY
Release: 2016-06-01 09:55:01
Original
2276 people have browsed it

第一种方法,通过元素的name获取

使用方法:

<code class="language-javascript">document.pref.color.value;//pref表示form表单的name,color是表单中元素的名称。</code>
Copy after login

实例:

<code class="language-html">

    <script language="JavaScript">
    function showColor(){
      var color = document.pref.color.value;
      console.log("Your favorite color is: " + color);
    }
    </script>
    
    
    <form name="pref" method="POST">
    Enter the name of your favorite car:
      <input type="text" name="car" size="25">
      <input id="button1" type="BUTTON" name="carButton" value="Show Car" onclick="showCar(this.form)">
    <br>
    Enter your favorite color:
      <input type="text" name="color" size="15">
      <input type="BUTTON" name="colorButton" value="Show Color" onclick="showColor(this.form)">
    </form>

</code>
Copy after login

上面实例代码使用ocument.pref.color.value获取pref表单中name为color元素的值。

 

第二种方法:通过元素的index获取

使用方法:

<code class="language-javascript">document.pref.elements[0].value //表示name为pref表单中第一个元素的值</code>
Copy after login

实例:

<code class="language-html">

    <script language="JavaScript">
    function showCar(){
      var car = document.pref.elements[0].value;
      console.log("Your favorite car is: " + car);
    }
    </script>
    
    
    <form name="pref" method="POST">
    Enter the name of your favorite car:
      <input type="text" name="car" size="25">
      <input id="button1" type="BUTTON" name="carButton" value="Show Car" onclick="showCar(this.form)">
    <br>
    Enter your favorite color:
      <input type="text" name="color" size="15">
      <input type="BUTTON" name="colorButton" value="Show Color" onclick="showColor(this.form)">
    </form>

</code>
Copy after login

此实例获取了表单中第一个元素的值,表单的第一个元素应该是name为car的input

 

第三种方法:通过元素的id获取

<code class="language-javascript">document.getElementById("id").value;</code>
Copy after login

实例:

<code class="language-html">
    
    <script language="JavaScript">
    function getName(){
         var textName = document.getElementById("text_id").value;
         console.log("The textbox name is: " + textName);
    }
    </script>
    <form name="form1">
    This is a blank input textbox. Click on the button below to get the name of the textbox.
    <br>
    <input type="text" id="text_id" name="textbox1" size="25">
    <br><br>
    <input type="button" value="Get Name" onclick="getName()">
    </form>
    
</code>
Copy after login

使用document.getElementById("text_id")获取id为text_id的input.

注:本文章的实例代码均可复制到这里运行并查看结果,你不妨试一把。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template