我的 javascript 代码中有几个选择器。其中之一是生成另一个选择器的触发器:
<form id="reportform" name="reportform" action="">
<table cellpadding="3" cellspacing="16">
<thead>
<th class="report" colspan="2">Select Scope</th>
<th class="report" colspan="2">Select Date</th>
<th class="report" colspan="2">Options</th>
</thead>
<tbody>
<tr>
...
<td>
Period
</td>
<td>
<select id="period" name="period">
<option selected="selected" value="0">- Select -</option>
<option value="1">Day</option>
<option value="2">Month</option>
<option value="3">Year</option>
</select>
</td>
...
</tr>
...
<tr>
<td></td>
<td></td>
<td id="date-label2"></td>
<td id="date2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align="right">
<input id="go" class="go" type="submit" value="Report">
</td>
</tr>
</tbody>
</table>
</form>
When one of the options in period select is selected one function is executed:
$('#period').change(function() {
switch($('#period').find(":selected").text()){
case 'Day':
clearDates();
var label = document.getElementById("date-label");
var txt = document.createTextNode("day");
label.appendChild(txt);
var day = document.getElementById("date");
var input = document.createElement("input");
input.className = "day-report";
day.appendChild(input);
break;
case 'Month':
...
case 'Year':
...
}
});
这里使用的函数:
function clearDates(){
var date = document.getElementById("date-label");
while (date.hasChildNodes()) {
date.removeChild(date.lastChild);
}
date = document.getElementById("date");
while (date.hasChildNodes()) {
date.removeChild(date.lastChild);
}
date = document.getElementById("date-label2");
while (date.hasChildNodes()) {
date.removeChild(date.lastChild);
}
date = document.getElementById("date2");
while (date.hasChildNodes()) {
date.removeChild(date.lastChild);
}
}
关键是当我单击提交按钮时,我看不到序列化表单的动态元素,我想不会发送到服务器。
$(function(){
$("#reportform").submit(function(event){
event.preventDefault();
alert(JSON.stringify($('form').serializeObject()));
return false;
});
});
表单有可能获取那些动态字段吗?提前致谢