我想使用jquery建立一个动态的添加/删除表单。
IT应该看起来像:
IT应该看起来像:
名称类型是否必需?
示例输入:
>名称类型是否必需?
>托管管理员选中(复选框)删除< ==按按钮将删除该行
我有一个添加/删除输入框的示例如何才能转换到我的想法?我必须使用多个coloumn表吗?谢谢你的帮助
<html> <head> <title>jQuery add / remove textBox example</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <style type="text/css"> div{ padding:8px; } </style> </head> <body> <h1>jQuery add / remove textBox example</h1> <script type="text/javascript"> $(document).ready(function(){ var counter = 2; $("#addButton").click(function () { if(counter>10){ alert("Only 10 textBoxes allow"); return false; } var newTextBoxDiv = $(document.createElement('div')) .attr("id",'TextBoxDiv' + counter); newTextBoxDiv.after().html('<label>TextBox #'+ counter + ' : </label>' + '<input type="text" name="textBox' + counter + '" id="textBox' + counter + '" value="" >'); newTextBoxDiv.appendTo("#TextBoxesGroup"); counter++; }); $("#removeButton").click(function () { if(counter==1){ alert("No more textBox to remove"); return false; } counter--; $("#TextBoxDiv" + counter).remove(); }); $("#getButtonValue").click(function () { var msg = ''; for(i=1; i<counter; i++){ msg += "\n TextBox #" + i + " : " + $('#textBox' + i).val(); } alert(msg); }); }); </script> </head><body> <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <label>TextBox #1 : </label><input type='textBox' id='textBox1' > </div> </div> <input type='button' value='Add Button' id='addButton'> <input type='button' value='Remove Button' id='removeButton'> <input type='button' value='Get TextBox Value' id='getButtonValue'> </body> </html>
解决方法
我自由地放置一个jsFiddle说明使用jQuery构建自定义窗体的功能。
Here it is…
编辑:根据最后评论中的请求,这里是来自jsFiddle的代码:
HTML:
<fieldset id="buildyourform"> <legend>Build your own form!</legend> </fieldset> <input type="button" value="Preview form" class="add" id="preview" /> <input type="button" value="Add a field" class="add" id="add" />
JavaScript:
$(document).ready(function() { $("#add").click(function() { var intId = $("#buildyourform div").length + 1; var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>"); var fName = $("<input type=\"text\" class=\"fieldname\" />"); var fType = $("<select class=\"fieldtype\"><option value=\"checkBox\">Checked</option><option value=\"textBox\">Text</option><option value=\"textarea\">Paragraph</option></select>"); var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />"); removeButton.click(function() { $(this).parent().remove(); }); fieldWrapper.append(fName); fieldWrapper.append(fType); fieldWrapper.append(removeButton); $("#buildyourform").append(fieldWrapper); }); $("#preview").click(function() { $("#yourform").remove(); var fieldSet = $("<fieldset id=\"yourform\"><legend>Your Form</legend></fieldset>"); $("#buildyourform div").each(function() { var id = "input" + $(this).attr("id").replace("field",""); var label = $("<label for=\"" + id + "\">" + $(this).find("input.fieldname").first().val() + "</label>"); var input; switch ($(this).find("select.fieldtype").first().val()) { case "checkBox": input = $("<input type=\"checkBox\" id=\"" + id + "\" name=\"" + id + "\" />"); break; case "textBox": input = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" />"); break; case "textarea": input = $("<textarea id=\"" + id + "\" name=\"" + id + "\" ></textarea>"); break; } fieldSet.append(label); fieldSet.append(input); }); $("body").append(fieldSet); }); });
CSS:
body { font-family:Gill Sans MT; padding:10px; } fieldset { border: solid 1px #000; padding:10px; display:block; clear:both; margin:5px 0px; } legend { padding:0px 10px; background:black; color:#FFF; } input.add { float:right; } input.fieldname { float:left; clear:left; display:block; margin:5px; } select.fieldtype { float:left; display:block; margin:5px; } input.remove { float:left; display:block; margin:5px; } #yourform label { float:left; clear:left; display:block; margin:5px; } #yourform input,#yourform textarea { float:left; display:block; margin:5px; }