Input Text value attribute
Definition and usage
value
Attribute setting or returning the value of the text field's value attribute.
The HTML value attribute contains a default value or a value entered by the user (or a value set by the script).
See also:
HTML Reference Manual:HTML <input> value 属性
Instance
Example 1
Change the value of the text field:
document.getElementById("myText").value = "Bill Gates";
More examples are provided below the page.
Syntax
Return the value attribute:
textObject.value
Set the value attribute:
textObject.value = text
Attribute value
Value | Description |
---|---|
text | Specify the value of the input text field. |
Technical details
Return value: | String value, representing the value of the text field. |
---|
More examples
Halimbawa 2
Get the value of the text field:
var x = document.getElementById("myText").value;
Halimbawa 3
Form validation:
var at = document.getElementById("email").value.indexOf("@"); var age = document.getElementById("age").value; var fname = document.getElementById("fname").value; submitOK = "true"; if (fname.length > 10) { alert("The name may have no more than 10 characters"); submitOK = "false"; if (isNaN(age) || age < 1 || age > 100) { alert("The age must be a number between 1 and 100"); submitOK = "false"; if (at == -1) { alert("Not a valid e-mail!"); submitOK = "false"; if (submitOK == "false") { return false;
Halimbawa 4
Dropdown list sa form:
var mylist = document.getElementById("myList"); document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
Halimbawa 5
Ibang dropdown list:
var no = document.getElementById("no"); var option = no.options[no.selectedIndex].text; var txt = document.getElementById("result").value; txt = txt + option; document.getElementById("result").value = txt;
Halimbawa 6
Ipinapakita ang halimbawa ng pagkakaiba sa pagitan ng mga attribute na defaultValue at value:
var x = document.getElementById("myText"); var defaultVal = x.defaultValue; var currentVal = x.value;
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |