Add and delete the text box for adding information on the page]

Source: Internet
Author: User

Content on the page:

When you click add or delete, the corresponding operation will be performed.

 

HTML code of the page

1 <li id = "clid"> 2 <div> 3 name: <input type = "text" name = "guestname" class = "dfinput" style = "width: 150px; "/> 4 & nbsp; Country: <input name =" gueststate "type =" text "class =" dfinput "style =" width: 150px; "> 5 & nbsp; Gender: & nbsp; <select name = "guestsex"> 6 <option value = "0"> select </option> 7 <option value = "1"> male </option> 8 <Option value = "2"> female </option> 9 </SELECT> 10 & nbsp; & nbsp; Unit <input type = "text" name = "gusetunit" class = "dfinput" style = "width: 150px; "/> 11 & nbsp; Title: <input type = "text" name = "guestjob" class = "dfinput" style = "width: 150px; "/> 12 <input type =" button "value =" delete "onclick =" del (this ); "> <input type =" button "value =" add "onclick =" add (); "> 13 </div> 14 </LI>
View code

JavaScript code for dynamic addition and deletion (jquery)

1 <SCRIPT type = "text/JavaScript"> 2 // dynamically add External Object 3 function add () {4 // obtain the object to be cloned 5 var clonediv =$ ("# clid Div "). eq (0); 6 // clone 7 var newdiv = clonediv. clone (); 8 // Add 9 // Add a blank value before 10 $ ("input [type = 'text']", newdiv ). val (""); 11 // Add 12 newdiv. appendto ("# clid"); 13 14} 15 16 17 // Delete 18 function del (BTN) {19 // get the object to be deleted 20 var deldiv =$ (BTN ). parent (); 21 // when the last one is deleted, 22 var lastdiv =$ ("# clid Div"); 23 if (lastdiv. length> 1) {24 deldiv. remove (); 25} 26} 27 </SCRIPT>
View code

 

Add and delete the text box for adding information on the page]

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.