我正在构建一个允许用户插入调查/民意调查的工具。默认情况下,我为可能的答案提供了四个文本框。但是,如果调查需要更多答案选项,我会使用 JQuery clone()。但是,当用户单击以克隆额外的答案字段时,无法使用 Asp 将值传递到数据库中,就好像创建的文本字段不存在一样。
我的代码在这里工作:http: //jsfiddle.net/LAECx/120/
渲染的 HTML 标记
<table id='pollcreate' width="100%" cellpadding="2" cellspacing="0" border="0" >
<tbody>
<form name="pollcreation_form" action="pollcreate_action.asp" method="post">
<tr id="qrow_poll_answer" name="qrow_poll_answer">
<td id="qcell_poll_answer" name="qcell_poll_answer" align="left" valign="top" class="formlabelcell" >Answer Option 1</td>
</tr>
<tr id="arow_poll_answer" name="arow_poll_answer">
<td id="acell_poll_answer" name="acell_poll_answer" align="left" valign="top" class="formquestioncell" ><input type="text" id="poll_answer" name="poll_answer" value="" size="50" maxlength="500" class="forminputtext" ></td>
</tr>
<tr id="sprow_poll_answer" name="sprow_poll_answer">
<td id="spcell_poll_answer" name="spcell_poll_answer" align="left" valign="top"><img src="http://img.lightreading.com/images/spacer.gif" width="1" height="8" border="0"></td>
</tr>
<tr id="brow_addrow" name="brow_addrow">
<td align="left" valign="top" class="formfullcell" ><input type="submit" name="addrow" id="addrow" value="Add Answer Option" class="forminputbutton" ></td>
</tr>
<tr id="brow_submit" name="brow_submit">
<td align="left" valign="top" class="formfullcell" ><input type="submit" name="submit" id="submit" value="Create" class="forminputbutton" ></td>
</tr>
</tbody>
</table>
jQuery
$(document).ready(function() {
var index = 5;
$("#addrow").click(function() {
var row = $('#pollcreate tbody tr#qrow_poll_answer:last, #pollcreate tbody tr#arow_poll_answer:last, #pollcreate tbody tr#sprow_poll_answer:last').clone(true).insertAfter('#pollcreate tbody tr#sprow_poll_answer:last');
$("td.formlabelcell:eq(0)", row).text("Answer Option " + index)
$("td.formquestioncell:eq(0) input", row).attr("name", "poll_answer" + index).attr("id", "poll_answer" + index).attr("value", index)
index++;
return false;
});
});