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

Source: Internet
Author: User

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

Code to copy code as follows
<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= "+" 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 text box 1 to text box 2" Onclick= "Get1 ();"/></td>
</tr>
<tr>
<TD align= "center" ><textarea name= "Txtbox2" cols= "+" rows= "5" id= "Txtbox2" ></textarea><br/ > Text Box 2</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</form>
</table>


With the above code, we can simply 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 text lengths in a text box:

Code to copy code as follows
<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.leng (www.111cn.net) th+ "n"
+ "The length of Txt2 is" +otextbox2.value.length);
}
</script>
<body>
<input type= "text" size= "id=" txt1 "/><br/>

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


This example uses the length property of value to determine the number of characters in each text box

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

jquery Gets the value of a text box

is through

Code to copy code as follows
var val = $ ("#drugGeneralName"). Val ();

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


The text box sets a default value. When the cursor moves to a text box, empty if the current value of the text box is the default value, or if the text box value is empty when you leave the text box, set the text box value to the default value. This is the way to achieve this idea.

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

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

Finally, when you make a form submission, you must first determine the value of the current text box. If it is the default value, you should not commit, but give the appropriate hint
From:http://www.111cn.net/wy/js-ajax/49622.htm

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

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.