Jquery enables dynamic addition and deletion of input boxes

Source: Internet
Author: User
The main function is to dynamically increase or decrease input boxes, and supports detection of each input box. After entering the content in each input box, the system prompts the error and concatenates all the content in the input box in the format of 1234235 #34634234 #123525. Webpage code: 01 & lt; fo... SyntaxHighlighter. all (

 

The main function is to dynamically increase or decrease input boxes, and supports detection of each input box.

After entering the content in each input box, an error is prompted.

Finally, We splice all the content in the input box with the format "1234235 #34634234 #123525.

 

Webpage code:

 

01

Js Code:

 

01

$ (Function (){

02

// Initialization

03

InitFields ();

04

});

05

 

06

 

07

Function initFields (){

08

// Initialize the mobile phone

09

Var text = $ ('# mobile'). val ();

10

Var ss = text. split ("#");

11

If (ss. length> 0 ){

12

$ ('# Mobile_t'). val (ss [0]);

13

}

14

For (var I = 1; I

15

Addinput ('mobile _ s_div ', 'mobile _ div', ss [I]);

16

}

17

18

19

}

20

/**

21

String concatenation

22

Src_id is the name of the source input, and dist_id is the id value of the target input.

23

*/

24

Function compose (src_name, dist_id ){

25

Var str = "";

26

Var ss = $ ('input [name = '+ src_name +'] '). each (function (I ){

27

If ($ (this). val ()! = "")

28

Str + = '#' + $ (this). val ();

29

});

30

Str = str. substring (1, str. length );

31

$ ('#' + Dist_id). val (str );

32

}

33

 

34

/**

35

Clone an input and display it in the specified container.

36

*/

37

Function addinput (id, div_id, text ){

38

Var mobile_div = $ ('#' + id). clone ();

39

Mobile_div.children ('input'). val (text );

40

Var delbt = $ ("")

41

Delbt. bind ("click", function () {$ (this). parent (). remove ()});

42

Mobile_div.append (delbt)

43

$ ('#' + Div_id). append (mobile_div );

44

Return false;

45

}

46

 

47

Function check1 (){

48

Compose ('mobile _ t', 'mobile ');

49

}

50

 

51

//

52

Function checkMobilephone (obj ){

53

54

If (obj = null ){

55

Return false;

56

}

57

58

Var span = $ (obj). parent (). children ('span ');

59

60

Var str = obj. value;

61

If (str = ""){

62

Span. text ('');

63

$ (Obj). removeClass ("inputError ");

64

Return false;

65

}

66

67

Var pattern =/^ 1 \ d {10} $ /;

68

69

If (! Pattern.exe c (obj. value) {// does not match.

70

Span. text ('mobile phone number entered incorrectly! '0000.css ("color", "red ");

71

$ (Obj). addClass ("inputError ");

72

Return false;

73

} Else {

74

Span. text ('');

75

$ (Obj). removeClass ("inputError ");

76

}

77

}

Introduce js files into html files

 

1

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.