JavaScript dynamic Add Form option example

Source: Internet
Author: User
Tags prev

To do a questionnaire, the number of questions is uncertain, answer questions and answers uncertain.

To achieve this effect, click on the button below to add 4 answer boxes, choose from A-Z 26 letters, is to Assic code to facilitate processing

Look at the HTML results

The code is as follows Copy Code

<table width= "100%" class= "form" >
<tr>
Th width= "100px" ><label> title 1:</label></th>
&LT;TD align= "left" ><input type= "text" class= "large" id= "title" Name= "title"/>&nbsp;<span class= " Error > Please enter the title </span></td>
</tr>
<tr>
<th width= "100px" ><label> selection type:</label></th>
&LT;TD align= "Left" >
<input type= "Radio" name= "Radio"/> Radio &nbsp;&nbsp;&nbsp;
<input type= "Radio" name= "Boxcheck"/> Multiple selection &nbsp;&nbsp;&nbsp;
<input type= "Radio" name= "select"/> dropdown &nbsp;&nbsp;&nbsp;
</td>
</tr>
<tr>
<th width= "100px" ><label> answer:</label></th>
&LT;TD align= "Left" >
<div>
<div style= "Float:left" id= "1" >
A:<input type= "text" class= "Mimi" Name= "A"/>
B:<input type= "text" class= "Mimi" Name= "B"/>
C:<input type= "text" class= "Mimi" Name= "C"/>
D:<input type= "text" class= "Mimi" Name= "D"/>
</div>
</div>
<div class= "More" onclick= "Add_ask ($ (this))" >&nbsp;</div>
</td>

</tr>

</table>

Through this HTML result above, and then through JS implementation

function Add_ask ($this)
{
var $Word = "";
Get Front div layer
var $div _num = $this. Prev (). Children (). attr ("id");
Add Layers
var $div _next_num = number ($div _num) +1;

var $last _children_name = $this. Prev (). Children (). Last (). Children (). Last (). attr ("name"). charCodeAt ();

var $html = "<div style=\" float:left;\ "id=" + $div _next_num+ ">";
Cycle four times
if ($last _children_name+4 <= 90)
{
for (var $i =1; $i <=4; $i + +)
{
$Word = String.fromCharCode ($last _children_name + $i);
$html + + $Word + ": <input type=\" text\ "class=\" mimi\ "name=" + $Word + "/>";
}
}else
{
$end = 90-$last _children_name;
for (var $i =1; $i <= $end; $i + +)
{
$Word = String.fromCharCode ($last _children_name + $i);
$html + + $Word + ": <input type=\" text\ "class=\" mimi\ "name=" + $Word + "/>";
}
}
$html + + "</div>";

$this. Prev (). append ($html);
}

The above JS is implemented through jquery, the principle is very simple answer, I don't say too much.
Clever place is through Assic code digital conversion to achieve the option to increase

Let's look at one more jquery example

The code is as follows Copy Code

<script type= "Text/javascript" src= "Js/jquery1.7.js" ></script>
<script type= "Text/javascript" >
$ (function () {
var show_count = 20; Number of bars to display
var count = 1; Increment start value, here is your ID
$ ("#btn_addtr"). Click (function () {

var length = $ ("#dynamicTable tbody tr"). Length;
alert (length);
if (length < Show_count)//Click Time, if the current number is less than the incremental end condition
{
$ ("#tab11 tbody tr"). Clone (). Appendto ("#dynamicTable tbody"); Add a row after the table
Changeindex ()//Update line number
}
});


});
function Changeindex () {
var i = 1;
$ ("#dynamicTable tbody tr"). each (function () {//cyclic tab TBODY TR
$ (this). Find ("Input[name= ' NO ']"). Val (i++);//Update line number
});
}

function deltr (OPP) {
var length = $ ("#dynamicTable tbody tr"). Length;
alert (length);
if (length <= 1) {
Alert ("Keep at least one row");
} else {
$ (OPP). Parent (). Parent () remove ();//Remove When moving forward
Changeindex ();
}
}
</script>

<body>

<div style= "width:720px;margin:20px auto;" >

<table id= "Tab11" style= "Display:none" >
<tbody>
<tr>
&LT;TD height= align= "center" >
<input type= "text" name= "NO" size= "2" value= "1"/></td>
&LT;TD align= "center" >
<input type= "text" name= "Start_end_time"/></td>
&LT;TD align= "center" >
<input type= "text" name= "Unit_department"/></td>
&LT;TD align= "center" >
<input type= "text" name= "POST"/></td>
<td>
<input type= "button" id= "Button1" onclick= "deltr (This)" value= "Delete Line" >
</td>
</tr>
</tbody>
</table>
<input type= "button" id= "BTN_ADDTR" value= "Add Line" >
<table id= "dynamictable" width= "M" border= "0" cellspacing= "0" cellpadding= "0" >
<thead>
<tr>
&LT;TD height= align= "center" bgcolor= "#CCCCCC" >ID</td>
&LT;TD align= "center" bgcolor= "#CCCCCC" > Starting and Ending Time </td>
&LT;TD align= "center" bgcolor= "#CCCCCC" > unit/Department </td>
&LT;TD align= "center" bgcolor= "#CCCCCC" > Position </td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
&LT;TD height= align= "center" >
<input type= "text" name= "NO" size= "2" value= "1"/></td>
&LT;TD align= "center" >
<input type= "text" name= "Start_end_time"/></td>
&LT;TD align= "center" >
<input type= "text" name= "Unit_department"/></td>
&LT;TD align= "center" >
<input type= "text" name= "POST"/></td>
<td>
<input type= "button" id= "Button2" onclick= "deltr (This)" value= "Delete Line" >
</td>
</tr>
</tbody>
</table>

</div>

</body>

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.