用JS怎样获取文本框的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JS怎样获取文本框的值相关的知识,希望对你有一定的参考价值。

如题
<script language="avascript">
function chk();

</script>
<form name=form1>
<input name=t1 type="text">
</form>

代码示例:

document.getElementById("id").innerhtml //获取文本的内容;

document.getElementById("id").value //获取文本框的内容;

function getValue()

var input=document.getElementById("myId");//通过id获取文本框对象

alert(input.value);//通过文本框对象获取value值


扩展资料:

javascript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果。通常JavaScript脚本是通过嵌入在HTML中来实现自身的功能的。

是一种解释性脚本语言(代码不进行预编译)。 [4] 

主要用来向HTML(标准通用标记语言下的一个应用)页面添加交互行为。 [4] 

可以直接嵌入HTML页面,但写成单独的js文件有利于结构和行为的分离。 [4] 

跨平台特性,在绝大多数浏览器的支持下,可以在多种平台下运行(如Windows、Linux、Mac、androidios等)。

Javascript脚本语言同其他语言一样,有它自身的基本数据类型,表达式和算术运算符及程序的基本程序框架。Javascript提供了四种基本的数据类型和两种特殊数据类型用来处理数据和文字。而变量提供存放信息的地方,表达式则可以完成较复杂的信息处理

参考资料:JavaScript-百度百科

参考技术A

当已知文本框的id属性时,<input type="text" id="userName">

方法一:var userName = $("#userName").attr("value");

方法二:var userName=$("userName").value();

<input type="text" name="one" value="5"/>用jquery怎么得值所以正确的方法应该是

<input type="text" id="one" value="5" />

$("#one").val();

拓展资料

作用是快速获取文档元素,jQuery的选择机制构建于Css的选择器,它提供了快速查询DOM文档中元素的能力,而且大大强化了JavaScript中获取页面元素的方式。

提供漂亮的页面动态效果,jQuery中内置了一系列的动画效果,可以开发出非常漂亮的网页,许多网站都使用jQuery的内置的效果,比如淡入淡出、元素移除等动态特效。

参考技术B

1)原生js

元素.value; //获取input里的值

元素.value = 'xxx' ;//给input设置值


2)Jquery/需要导包

元素.val();//获取input元素的值

元素.val('xxx');//给input设置值

下面例子:


<body>
<input value="test">
<script src="jquery-2.1.1.min.js"></script>
<script>
    //原生js
    console.log(document.getElementsByTagName('input')[0].value);//打印出test
    document.getElementsByTagName('input')[0].value = 'test2';//把文本框里的值设为test2

    //Jquery
    console.log($('input').val());//打印出test2
    $('input').val('test');//把文本框里的值设为test

</script>
</body>

参考技术C document.forms.0.thistext.value

Table of Contents | Previous | Next | Index

Text
A text input field on an HTML form. The user can enter a word, phrase, or series of numbers in a text field.

Client-side object

Implemented in

JavaScript 1.0

JavaScript 1.1: added type property

JavaScript 1.2: added handleEvent method

Created by
The HTML INPUT tag, with "text" as the value of the TYPE attribute. For a given form, the JavaScript runtime engine creates appropriate Text objects and puts these objects in the elements array of the corresponding Form object. You access a Text object by indexing this array. You can index the array either by number or, if supplied, by using the value of the NAME attribute.

To define a Text object, use standard HTML syntax with the addition of JavaScript event handlers.

Event handlers

* onBlur
* onChange
* onFocus
* onSelect

Description
A Text object on a form looks as follows:

A Text object is a form element and must be defined within a FORM tag.

Text objects can be updated (redrawn) dynamically by setting the value property (this.value).

Property Summary

Property Description

defaultValue

Reflects the VALUE attribute.

form

Specifies the form containing the Text object.

name

Reflects the NAME attribute.

type

Reflects the TYPE attribute.

value

Reflects the current value of the Text object's field.
Method Summary

Method Description

blur

Removes focus from the object.

focus

Gives focus to the object.

handleEvent

Invokes the handler for the specified event.

select

Selects the input area of the object.
In addition, this object inherits the watch and unwatch methods from Object.

Examples
Example 1. The following example creates a Text object that is 25 characters long. The text field appears immediately to the right of the words "Last name:". The text field is blank when the form loads.

<B>Last name:</B> <INPUT TYPE="text" NAME="last_name" VALUE="" SIZE=25>

Example 2. The following example creates two Text objects on a form. Each object has a default value. The city object has an onFocus event handler that selects all the text in the field when the user tabs to that field. The state object has an onChange event handler that forces the value to uppercase.

<FORM NAME="form1">
<BR><B>City: </B><INPUT TYPE="text" NAME="city" VALUE="Anchorage"
SIZE="20" onFocus="this.select()">
<B>State: </B><INPUT TYPE="text" NAME="state" VALUE="AK" SIZE="2"
onChange="this.value=this.value.toUpperCase()">
</FORM>

See also the examples for the onBlur, onChange, onFocus, and onSelect.

See also
Text, Form, Password, String, Textarea

blur
Removes focus from the text field.

Method of

Text

Implemented in

JavaScript 1.0

Syntax

blur()

Parameters
None

Examples
The following example removes focus from the text element userText:

userText.blur()

This example assumes that the text element is defined as

<INPUT TYPE="text" NAME="userText">

See also
Text.focus, Text.select

defaultValue
A string indicating the default value of a Text object.

Property of

Text

Implemented in

JavaScript 1.0

Security
JavaScript 1.1. This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description
The initial value of defaultValue reflects the value of the VALUE attribute. Setting defaultValue programmatically overrides the initial setting.

You can set the defaultValue property at any time. The display of the related object does not update when you set the defaultValue property, only when you set the value property.

Examples
The following function evaluates the defaultValue property of objects on the surfCity form and displays the values in the msgWindow window:

function defaultGetter()
msgWindow=window.open("")
msgWindow.document.write("hidden.defaultValue is " +
document.surfCity.hiddenObj.defaultValue + "<BR>")
msgWindow.document.write("password.defaultValue is " +
document.surfCity.passwordObj.defaultValue + "<BR>")
msgWindow.document.write("text.defaultValue is " +
document.surfCity.textObj.defaultValue + "<BR>")
msgWindow.document.write("textarea.defaultValue is " +
document.surfCity.textareaObj.defaultValue + "<BR>")
msgWindow.document.close()


See also
Text.value

focus
Navigates to the text field and gives it focus.

Method of

Text

Implemented in

JavaScript 1.0

Syntax

focus()

Parameters
None

Description
Use the focus method to navigate to a text field and give it focus. You can then either programmatically enter a value in the field or let the user enter a value. If you use this method without the select method, the cursor is positioned at the beginning of the field.

Examples
See example for select.

See also
Text.blur, Text.select

form
An object reference specifying the form containing this object.

Property of

Text

Read-only

Implemented in

JavaScript 1.0

Description
Each form element has a form property that is a reference to the element's parent form. This property is especially useful in event handlers, where you might need to refer to another element on the current form.

Examples
Example 1. In the following example, the form myForm contains a Text object and a button. When the user clicks the button, the value of the Text object is set to the form's name. The button's onClick event handler uses this.form to refer to the parent form, myForm.

<FORM NAME="myForm">
Form name:<INPUT TYPE="text" NAME="text1" VALUE="Beluga">
<P>
<INPUT NAME="button1" TYPE="button" VALUE="Show Form Name"
onClick="this.form.text1.value=this.form.name">
</FORM>

Example 2. The following example shows a form with several elements. When the user clicks button2, the function showElements displays an alert dialog box containing the names of each element on the form myForm.

function showElements(theForm)
str = "Form Elements of form " + theForm.name + ": \n "
for (i = 0; i < theForm.length; i++)
str += theForm.elements[i].name + "\n"
alert(str)

</script>
<FORM NAME="myForm">
Form name:<INPUT TYPE="text" NAME="text1" VALUE="Beluga">
<P>
<INPUT NAME="button1" TYPE="button" VALUE="Show Form Name"
onClick="this.form.text1.value=this.form.name">
<INPUT NAME="button2" TYPE="button" VALUE="Show Form Elements"
onClick="showElements(this.form)">
</FORM>

The alert dialog box displays the following text:

JavaScript Alert:
Form Elements of form myForm:
text1
button1
button2

Example 3. The following example uses an object reference, rather than the this keyword, to refer to a form. The code returns a reference to myForm, which is a form containing myTextObject.

document.myForm.myTextObject.form

See also
Form

handleEvent
Invokes the handler for the specified event.

Method of

Text

Implemented in

JavaScript 1.2

Syntax

handleEvent(event)

Parameters

event

The name of an event for which the specified object has an event handler.

name
A string specifying the name of this object.

Property of

Text

Implemented in

JavaScript 1.0

Security
JavaScript 1.1. This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description
The name property initially reflects the value of the NAME attribute. Changing the name property overrides this setting. The name property is not displayed on-screen; it is used to refer to the objects programmatically.

If multiple objects on the same form have the same NAME attribute, an array of the given name is created automatically. Each element in the array represents an individual Form object. Elements are indexed in source order starting at 0. For example, if two Text elements and a Textarea element on the same form have their NAME attribute set to "myField", an array with the elements myField[0], myField[1], and myField[2] is created. You need to be aware of this situation in your code and know whether myField refers to a single element or to an array of elements.

Examples
In the following example, the valueGetter function uses a for loop to iterate over the array of elements on the valueTest form. The msgWindow window displays the names of all the elements on the form:

newWindow=window.open("http://home.netscape.com")

function valueGetter()
var msgWindow=window.open("")
for (var i = 0; i < newWindow.document.valueTest.elements.length; i++)
msgWindow.document.write(newWindow.document.valueTest.elements[i].name + "<BR>")



select
Selects the input area of the text field.

Method of

Text

Implemented in

JavaScript 1.0

Syntax

select()

Parameters
None

Description
Use the select method to highlight the input area of a text field. You can use the select method with the focus method to highlight a field and position the cursor for a user response. This makes it easy for the user to replace all the text in the field.

Examples
The following example uses an onClick event handler to move the focus to a text field and select that field for changing:

<FORM NAME="myForm">
<B>Last name: </B><INPUT TYPE="text" NAME="lastName" SIZE=20 VALUE="Pigman">
<BR><B>First name: </B><INPUT TYPE="text" NAME="firstName" SIZE=20 VALUE="Victoria">
<BR><BR>
<INPUT TYPE="button" VALUE="Change last name"
onClick="this.form.lastName.select();this.form.lastName.focus();">
</FORM>

See also
Text.blur, Text.focus

type
For all Text objects, the value of the type property is "text". This property specifies the form element's type.

Property of

Text

Read-only

Implemented in

JavaScript 1.1

Examples
The following example writes the value of the type property for every element on a form.

for (var i = 0; i < document.form1.elements.length; i++)
document.writeln("<BR>type is " + document.form1.elements[i].type)


value
A string that reflects the VALUE attribute of the object.

Property of

Text

Implemented in

JavaScript 1.0

Security
JavaScript 1.1. This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description
The value property is a string that initially reflects the VALUE attribute. This string is displayed in the text field. The value of this property changes when a user or a program modifies the field.

You can set the value property at any time. The display of the Text object updates immediately when you set the value property.

Examples
The following function evaluates the value property of a group of buttons and displays it in the msgWindow window:

function valueGetter()
var msgWindow=window.open("")
msgWindow.document.write("submitButton.value is " +
document.valueTest.submitButton.value + "<BR>")
msgWindow.document.write("resetButton.value is " +
document.valueTest.resetButton.value + "<BR>")
msgWindow.document.write("myText.value is " +
document.valueTest.myText.value + "<BR>")
msgWindow.document.close()


This example displays the following:

submitButton.value is Query Submit
resetButton.value is Reset
myText.value is Stonefish are dangerous.

The previous example assumes the buttons have been defined as follows:

<INPUT TYPE="submit" NAME="submitButton">
<INPUT TYPE="reset" NAME="resetButton">
<INPUT TYPE="text" NAME="myText" VALUE="Stonefish are dangerous.">

See also
Text.defaultValue

Table of Contents | Previous | Next | Index

Last Updated: 05/28/99 12:00:41

Copyright (c) 1999 Netscape Communications Corporation

参考资料:http://devedge.netscape.com

参考技术D script language="avascript">
function chk()

var txtValue=document.form.t1.value;

</script>
<form name=form1>
<input name=t1 type="text">
<INPUT TYPE="submit" onclick="chk()">
</form>

可以得到t1的value。

如何用js把文本框清空?

<input type="text" id="region" runat="server" readonly="readonly" style="border:0px; font-weight:bold;"/>

document.getElementById("region").value="";

不行啊
这样。

应该用如下语言清空:


<head>
<script>
function clearDefaultText (el,message)
var obj = el;
if(typeof(el) == "string")
obj = document.getElementById(id);
if(obj.value == message)
obj.value = "";
obj.onblur = function()
if(obj.value == "")
obj.value = message;
</script>
</head>
<body>
<input type="text" id="searchKey" name="searchKey" value="请输入标题" onclick="clearDefaultText(this,'请输入标题')"/>
</body>

拓展资料


1、JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。


2、在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成。因为Netscape与Sun合作,Netscape管理层希望它外观看起来像Java,因此取名为JavaScript。但实际上它的语法风格与Self及Scheme较为接近。


3、为了取得技术优势,微软推出了JScript,CEnvi推出ScriptEase,与JavaScript同样可在浏览器上运行。为了统一规格,因为JavaScript兼容于ECMA标准,因此也称为ECMAScript。

参考技术A 用js把文本框清空只需要在input定义中加入clientidmode="Static",即将<inputtype="text" id="region" runat="server"readonly="readonly" style="border:0px; font-weight:bold;"/>改为<input type="text"id="region" runat="server" clientidmode="Static" readonly="readonly"style="border:0px; font-weight:bold;"/>,这样脚本生成的id为region。
再利用JS清空值,document.getElementById("region").value="";
参考技术B asp.net 服务器控件 生成静态页以后啊,id 的值会发生变化,

运行后,你看看html源代码 textbox 生成的id 值是什么

然后在用getElementById("生成的值").value 处理~
参考技术C readonly="readonly"
只读的,是没办法修改的,要把readonly="readonly"删除
参考技术D 这个是可以的。在按钮事件中调用这个方法
function clean()

document.getElementById("region").value="";
本回答被提问者采纳

以上是关于用JS怎样获取文本框的值的主要内容,如果未能解决你的问题,请参考以下文章

js怎样将一个文本框里的时间设成某日期的一个月后

html中,怎样用js或者jQuery去判断文本框输入的文本内容超过了文本框的宽度。

js获得文本框的值怎么转化成数值型啊

文本框的值改变了,js怎么获取

java中用a标签提交,后台怎样获取页面文本框的值

yii中怎样用jq得到下拉框的值并赋值给文本框