×
参考手册概览

JavaScript 对象

JavaScript Array 对象手册 JavaScript Array 对象JavaScript Boolean 对象JavaScript Date 对象JavaScript Math 对象JavaScript Number 对象JavaScript String 对象JavaScript RegExp 对象JavaScript 全局属性/函数JavaScript 运算符

Browser 对象

Window 对象Navigator 对象Screen 对象History 对象Location 对象

DOM 对象

HTML DOM DocumentHTML DOM 元素对象HTML DOM 属性对象HTML DOM 事件对象

HTML 对象

<a><area><audio><base><blockquote><body><button><canvas><col><colgroup><datalist><del><details><dialog><embed><fieldset><form><iframe><frameset ><img><ins><input> - button<input> - checkbox<input> - color<input> - date<input> - datetime<input> - datetime-local<input> - email<input> - file<input> - hidden<input> - image<input> - month<input> - number<input> - range<input> - password<input> - radio<input> - reset<input> - search<input> - submit<input> - text<input> - time<input> - url<input> - week<keygen><link><label><legend><li><map><menu><menuItem><meta><meter><object><ol><optgroup><option><param><progress><q><script><select><source><style><table><td><th><tr><textarea><title><time><track><video>

Input Text value 属性


HTML DOM Input Text 对象HTML DOM Input Text 对象


实例

修改文本域的值:

document.getElementById("myText").value = "Johnny Bravo";

尝试一下 »


定义和用法

value 属性可设置或者返回文本域的 value 属性值。

value 属性包含了默认值或用户输入的值(或通过脚本设置)。


浏览器支持

Internet Explorer Firefox Opera Google Chrome Safari

所有主流浏览器都支持 value 属性。


语法

返回 value 属性:

textObject.value

设置 value 属性:

textObject.value=text

属性值

描述
text 轨道了文本域的初始值

技术详情

返回值: 字符串,表示文本域的默认值


更多实例

实例

获取文本域的值:

var x = document.getElementById("myText").value;

x 输出结果为:

Mickey

尝试一下 »

实例

表单验证:

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("姓名不能超过 10 个字符");
submitOK = "false";
}
if (isNaN(age)||age<1||age>100) {
alert("年龄范围为 1 到 100");
submitOK = "false";
}
if (at==-1) {
alert("不合法的 e-mail!");
submitOK = "false";
}
if (submitOK=="false") {
return false;
}

尝试一下 »

实例

表单的下拉列表:

var mylist = document.getElementById("myList");
document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;

尝试一下 »

实例

另一个下拉列表:

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;

尝试一下 »


相关页面

HTML 参考手册: HTML <input> value 属性


HTML DOM Input Text 对象HTML DOM Input Text 对象


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)