Js/jquery gets the value of the text box and changes the value of the text box

Source: Internet
Author: User


We use it to learn to get the value of the text box and to change the value of the text box.

The code is as follows Copy Code

<script>
function Get1 ()
{
document.getElementById ("Txtbox2"). Value=document.getelementbyid ("Txtbox"). Value;

Gets the value of the text box 1 and assigns a value to the text box 2
}
</script>

<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<form id= "Form1" Name= "Form1" method= "Post" action= "" >
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<TD align= "center" ><textarea name= "Txtbox" cols= "a" rows= "5" id= "Txtbox" ></textarea><br/> text Box 1</td>
</tr>
<tr>
<TD height= "align=" center "><input type=" button "name=" button "id=" button "value=" Click to assign the value of TextBox 1 to text box 2 " Onclick= "Get1 ();"/></td>
</tr>
<tr>
<TD align= "center" ><textarea name= "Txtbox2" cols= "a" rows= "5" id= "Txtbox2" ></textarea><br > Text Box 2</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</form>
</table>


With the above code we can easily learn how to value and assign values.


Because the value attribute is a string, you can use the attributes and methods of any string. For example, you can use the length attribute to get the text in a text box:

The code is as follows Copy Code

<title>retrieving a Textbox Length example</title>
<script type= "Text/javascript" >
function Getlengths () {
var Otextbox1=document.getelementbyid ("Txt1");
var Otextbox2=document.getelementbyid ("Txt2");
Alert ("The length of Txt1 is" +otextbox1.value.length+ "n"
+ "The length of Txt2 is" +otextbox2.value.length);
}
</script>
<body>
<input type= "text" size= "" "Id=" Txt1 "/><br/>

<textarea rows= "5" cols= "a" id= "txt2" ></textarea><br/>
<input type= "button" value= "Get Lengths" onclick= "getlengths ()"/>
</body>


In this example, the length attribute of value is used to determine the number of characters in each text box.

Here's how jquery gets the value of the text box.

jquery Gets the value of the text box

is through

The code is as follows Copy Code
var val = $ ("#drugGeneralName"). Val ();

Val () method to get the value of the text box property.


The text box sets a default value. When the cursor is moved to a text box, empty if the current value of the text box is the default, and if the text box value is empty, the text box value is set to the default value. This is the way to realize the idea.

The code is as follows Copy Code

<input type= "text" class= "input" value= "Your Name"/>jquery code is as follows:

$ (document). Ready (function () {
Each traversal text box
$ (". Input"). each (function () {
Saves the value of the current text box
var vdefault = This.value;
$ (this). focus (function () {
If the value is the default value when the focus is obtained, the setting is null
if (this.value = = Vdefault) {
This.value = "";
}
});
$ (this). blur (function () {
If the value is blank when the focus is lost, set to the default value
if (This.value = = "") {
This.value = Vdefault;
}
});
});
});

Finally, when making a form submission, you first determine the value of the current text box. If you are the default, you should not commit, but give the appropriate hints.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.