我有一個2部分窗體。使用jquery,我如何提交使用AJAX創建的表單?
第一種形式是通過$ .post和(如果一切正確)提交的,由php中的一個動態生成的表單替換。這背後的想法是,第一種形式爲所述物品創建條目和相關圖像的數量。生成一個新表單來上傳所述圖像。
我的問題是我的新生成的表單沒有得到處理。如果通過舊的onClick =「」函數提交表單,動態生成的文件字段將被忽略。我很新的jquery/AJAX,所以我可能沒有做到這一點,但我怎麼能讓jquery注意我的第二個(動態生成的)表單?
1表格:
<form name="addinv" id="addinv" action="admin_process.php" method="post">
<fieldset>
<legend>Add Inventory</legend>
<input type="hidden" name="actiontotake" id="actiontotake" value="none" />
<table width="400" border="0">
<caption>
Add Inventory Form Layout Table
</caption>
<tr>
<td><strong>Price</strong></td>
<td><input type="text" name="price" id="price" tabindex="1" /></td>
</tr>
<tr>
<td><strong>Manufacturer</strong></td>
<td><input type="text" name="mfg" id="mfg" tabindex="2" /></td>
</tr>
<tr>
<td><strong>Model</strong></td>
<td><input type="text" name="model" id="model" tabindex="3" /></td>
</tr>
<tr>
<td><strong>Serial Number</strong></td>
<td><input type="text" name="sn" id="sn" tabindex="4" /></td>
</tr>
<tr>
<td><strong>Year</strong></td>
<td><input type="text" name="year" id="year" tabindex="5" /></td>
</tr>
<tr>
<td><strong>Size (Dimensions)</strong></td>
<td><input type="text" name="dimensions" id="dimensions" tabindex="6" /></td>
</tr>
<tr>
<td><strong>Bedrooms</strong></td>
<td><input type="text" name="beds" id="beds" tabindex="7" /></td>
</tr>
<tr>
<td><strong>Bathrooms</strong></td>
<td><input type="text" name="baths" id="baths" tabindex="8" /></td>
</tr>
<tr>
<td><strong>Range Type</strong></td>
<td><input type="text" name="range" id="range" tabindex="9" /></td>
</tr>
<tr>
<td><strong>Siding Type</strong></td>
<td><input type="text" name="siding" id="siding" tabindex="10" /></td>
</tr>
<tr>
<td><strong>Roof Type</strong></td>
<td><input type="text" name="roof" id="roof" tabindex="11" /></td>
</tr>
<tr>
<td><strong>Furnace Type</strong></td>
<td><input type="text" name="furnace" id="furnace" tabindex="12" /></td>
</tr>
<tr>
<td><strong>Features & Benefits</strong></td>
<td><textarea name="fandb" id="fandb" tabindex="13"></textarea></td>
</tr>
<tr>
<td><strong>Number of Pictures</strong></td>
<td><input type="text" name="picnum" id="dimensions" tabindex="14" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="addinventorybutton" id="addinventorybutton" type="submit" value="Add Home" tabindex="15"/></td>
</tr>
</table>
</fieldset>
</form>
jQuery的處理程序:
$(document).ready(
function(){
$("#addinventorybutton").click(
function(){
$("#actiontotake").val("Add Home");
var dta = $("#addinv").serialize();
$.post("admin_process.php",dta,function(data){
$("div#form").html(data);
});
return false;
});
$("#addnewpicturesbutton").click(
function(){
$("#actiontotake").val("Add Picture");
AddPic();
return false;
});
});
第二個表(通過PHP & AJAX調用):
<form name="addpictures" id="addpictures" enctype="multipart/form-data" method="post" action="admin_process.php">
<fieldset><legend>Add Associated Images</legend>
<?php
for($i=0;$i<$pics;$i++)
{
echo("<input type='file' name='pic".$i."' /><br />\n");
}
?>
<input type="hidden" name="actiontotake" id="actiontotake" value="none" />
<input type="hidden" name="inventory_id" id="inventory_id" value="<?php echo $newid; ?>">
<input type="button" name="addnewpicturesbutton" id="addnewpicturesbutton" value="Add Pictures to new home">
</fieldset>
</form>