Jquery allows you to dynamically increase or decrease the instance code in the input box.

Source: Internet
Author: User

Concatenates all the content in the input box in the format of "1234235 #34634234 #123525.
Webpage code:
Copy codeThe Code is as follows:
<Form>
<Table>
<Tr> <th> mobile phone: </th>
<Td style = "padding: 10px 0;">
<Input type = "hidden" name = "user. mobile" value = "124213 #243463" id = "mobile"/>
<Div id = "mobile_s_div">
<Input type = "text" id = "mobile_t" name = "mobile_t" value = "% {# session. user. mobile} "onchange =" checkMobilephone (this) "onkeyup =" checkMobilephone (this) "/>
<Span> </span>
</Div>
<Div id = "mobile_div"> </div>
<Input type = "submit" onclick = "return addinput ('mobile _ s_div ', 'mobile _ div ','') "value = 'add another'> </input>
</Td>
</Tr>
</Table>
</Form>

Js Code:
Copy codeThe Code is as follows:
$ (Function (){
// Initialization
InitFields ();
});
Function initFields (){
// Initialize the mobile phone
Var text = $ ('# mobile'). val ();
Var ss = text. split ("#");
If (ss. length> 0 ){
$ ('# Mobile_t'). val (ss [0]);
}
For (var I = 1; I <ss. length; I ++ ){
Addinput ('mobile _ s_div ', 'mobile _ div', ss [I]);
}
}
/**
String concatenation
Src_id is the name of the source input, and dist_id is the id value of the target input.
*/
Function compose (src_name, dist_id ){
Var str = "";
Var ss = $ ('input [name = '+ src_name +'] '). each (function (I ){
If ($ (this). val ()! = "")
Str + = '#' + $ (this). val ();
});
Str = str. substring (1, str. length );
$ ('#' + Dist_id). val (str );
}
/**
Clone an input and display it in the specified container.
*/
Function addinput (id, div_id, text ){
Var mobile_div = $ ('#' + id). clone ();
Mobile_div.children ('input'). val (text );
Var delbt = $ ("<input type = 'button 'value = 'delete'> </input> ")
Delbt. bind ("click", function () {$ (this). parent (). remove ()});
Mobile_div.append (delbt)
$ ('#' + Div_id). append (mobile_div );
Return false;
}
Function check1 (){
Compose ('mobile _ t', 'mobile ');
}
// <Input type = "text" onkeyup = "checkMobilephone (this)"/> <span/>
Function checkMobilephone (obj ){
If (obj = null ){
Return false;
}
Var span = $ (obj). parent (). children ('span ');
Var str = obj. value;
If (str = ""){
Span. text ('');
$ (Obj). removeClass ("inputError ");
Return false;
}
Var pattern =/^ 1 \ d {10} $ /;
If (! Pattern.exe c (obj. value) {// does not match.
Span. text ('mobile phone number entered incorrectly! '0000.css ("color", "red ");
$ (Obj). addClass ("inputError ");
Return false;
} Else {
Span. text ('');
$ (Obj). removeClass ("inputError ");
}
}

Introduce js files into html files
1
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "js/jquery-1.6.4.min.js"> </script>

2
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "js/index. js"> </script>

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.