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>