In js, we can use getElementById () to obtain the text box or change the value of the text box. The operation method in jquery is also simple and incorrect. Next I will introduce it to you.
We will use it to learn how to get the value of the text box and 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; // Obtain the value of text box 1 and assign it to text box 2 } </Script> <Table width = "500" 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 = "32" rows = "5" id = "txtbox"> </textarea> <br/> text box 1 </td> </Tr> <Tr> <Td height = "32" align = "center"> <input type = "button" name = "button" id = "button" value = "click to assign the value of text box 1 text Box 2 "onclick =" get1 (); "/> </td> </Tr> <Tr> <Td align = "center"> <textarea name = "txtbox2" cols = "32" 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 set values and assign values.
Because the value feature is a string, you can use any character string features and methods. For example, you can use the length feature to obtain the text length in the text box:
The Code is as follows: |
Copy code |
<Html> <Head> <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> </Head> <Body> <Input type = "text" size = "12" id = "txt1"/> <br/> <Textarea rows = "5" cols = "25" id = "txt2"> </textarea> <br/> <Input type = "button" value = "Get Lengths" onclick = "getLengths ()"/> </Body> </Html> |
In this example, the length attribute of value is used to determine the number of characters in each text box.
Next we will introduce jquery's method of getting the value of the text box.
JQuery obtains the value of the text box.
Yes
The Code is as follows: |
Copy code |
Var val = $ ("# drugGeneralName"). val (); |
Val () method to obtain the value of the text box attribute
Set a default value for the text box. When the cursor moves to the text box, if the current value of the text box is the default value, then empty; when the text box is left, if the text box value is empty, set the text box value to the default value. The implementation idea is like this.
The Code is as follows: |
Copy code |
<Input type = "text" class = "input" value = "your name"/> the jQuery code is as follows: $ (Document). ready (function (){ // Use each to traverse the text box $ (". Input"). each (function (){ // Save the value of the current text box Var vdefault = this. value; $ (This). focus (function (){ // When the focus is obtained, if the value is the default value, it is set to null. If (this. value = vdefault ){ This. value = ""; } }); $ (This). blur (function (){ // If the value is null when the focus is lost, it is set to the default value. If (this. value = ""){ This. value = vdefault; } }); }); }); |
Finally, when submitting a form, you must first determine the value of the current text box. If it is the default value, you should not submit it, but give the corresponding prompt.