目錄
在JavaScript 中取得輸入文字欄位值
首頁 web前端 js教程 如何在 JavaScript 中取得輸入文字欄位的值?

如何在 JavaScript 中取得輸入文字欄位的值?

Dec 21, 2024 pm 09:33 PM

How Do I Get the Value of an Input Text Field in JavaScript?

在JavaScript 中取得輸入文字欄位值

為了回應您的查詢,有多種方法可用於直接擷取輸入文字欄位的值,而無需使用表單。

方法一:使用getElementById()

document.getElementById('textbox_id').value 直接擷取所需文字輸入欄位的值。

document.getElementById("searchTxt").value; // Get the value of the searchTxt text field
登入後複製

方法2:使用getElementsByClassName()

document.getElementsByClassName('class_name')[whole_number].value 傳回一個可即時用於根據其類別選擇所需的文字欄位。

document.getElementsByClassName("searchField")[0].value; // Get the value of the first text field with class "searchField"
登入後複製

方法三:使用getElementsByTagName()

document.getElementsByTagName('input')[whole_number].value 也將返回一個實時您根據其標籤名稱選擇所需的文字欄位。

document.getElementsByTagName("input")[0].value; // Get the value of the first text input field
登入後複製

方法四:使用getElementsByName()

document.getElementsByName('name')[whole_number].value 傳回一個即時。 name 屬性選擇所需的文字欄位。

document.getElementsByName("searchTxt")[0].value; // Get the value of the first text field with name "searchTxt"
登入後複製

方法五:使用querySelector()

document.querySelector('selector').value 使用 CSS 選擇器來選擇所需的文字欄位。

document.querySelector('#searchTxt').value; // Get the value of the text field with id "searchTxt"
登入後複製

方法6:使用querySelectorAll()

document.querySelectorAll('selector')[whole_number].value 也使用CSS 選擇器來選擇所需的文字字段,但傳回靜態NodeList。

document.querySelectorAll('.searchField')[0].value; // Get the value of the first text field with class "searchField"
登入後複製

下表提供了這些方法的瀏覽器相容性資訊:

Browser Method 1 Method 2 Method 3 Method 4 Method 5/6
IE6 Buggy No Yes Buggy No
IE7 Buggy No Yes Buggy No
IE8 Yes No Yes Buggy Yes
IE9 Yes Yes Yes Buggy Yes
IE10 Yes Yes Yes Yes Yes
FF3.0 Yes Yes Yes Yes No
FF3.5/FF3.6 Yes Yes Yes Yes Yes
FF4b1 Yes Yes Yes Yes Yes
GC4/GC5 Yes Yes Yes Yes Yes and Yes
Safari4/Safari5 Yes Yes Yes Yes Yes
Opera10.10/
Opera10.53/ Yes Yes Yes Buggy Yes
Opera10.60
Opera 12 Yes Yes Yes Yes Yes

以上是如何在 JavaScript 中取得輸入文字欄位的值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在JavaScript中替換字符串字符 在JavaScript中替換字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替換字符串字符

自定義Google搜索API設置教程 自定義Google搜索API設置教程 Mar 04, 2025 am 01:06 AM

自定義Google搜索API設置教程

示例顏色json文件 示例顏色json文件 Mar 03, 2025 am 12:35 AM

示例顏色json文件

8令人驚嘆的jQuery頁面佈局插件 8令人驚嘆的jQuery頁面佈局插件 Mar 06, 2025 am 12:48 AM

8令人驚嘆的jQuery頁面佈局插件

10個jQuery語法熒光筆 10個jQuery語法熒光筆 Mar 02, 2025 am 12:32 AM

10個jQuery語法熒光筆

構建您自己的Ajax Web應用程序 構建您自己的Ajax Web應用程序 Mar 09, 2025 am 12:11 AM

構建您自己的Ajax Web應用程序

什麼是這個'在JavaScript? 什麼是這個'在JavaScript? Mar 04, 2025 am 01:15 AM

什麼是這個'在JavaScript?

10 JavaScript和JQuery MVC教程 10 JavaScript和JQuery MVC教程 Mar 02, 2025 am 01:16 AM

10 JavaScript和JQuery MVC教程

See all articles