How to dynamically add or remove instance code in the input box using jquery

Source: Internet
Author: User
The main function is to dynamically increase or decrease the input box, and supports detection of each input box. After each input box is input, an error message is displayed, concatenating strings to add all the content in the input box, splice the image in the format of "1234235 #34634234 #123525.
Webpage code:

The Code is as follows:




Js Code:

The 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 Addinput ('mobile _ s_p ', 'mobile _ P', 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, p_id, text ){
Var mobile_p = $ ('#' + id). clone ();
Mobile_p.children ('input'). val (text );
Var delbt = $ (" ")
Delbt. bind ("click", function () {$ (this). parent (). remove ()});
Mobile_p.append (delbt)
$ ('#' + P_id). append (mobile_p );
Return false;
}
Function check1 (){
Compose ('mobile _ t', 'mobile ');
}
//
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

The Code is as follows:


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.