使用此代码,ajax html被正确加载并且click事件会触发,但是当我尝试获取单击按钮的父窗体时,它似乎失败了。警报的输出未定义'。如何获取表单数据?
$( "#offertable" ).load( "offer-get.php", function() {
//$('#loadingDiv').hide();
$("#offertable").on('click', '#submit', function(event){
alert($(this).parents('form:first').attr("action"));
//event.preventDefault(); // this will prevent from submitting the form
//$(this).prop('disabled',true);
var postData = $(this).parents('form:first').serialize();
var formURL = $(this).parents('form:first').attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
alert("Offer updated successfully");
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("Failed to update offer");
}
});
return false;
});
} );
我已尝试在表单上使用提交事件(这会为我提供表单对象,我可以从中获取数据);
$("#offertable").on('submit', '#updateoffer', function(event){
但它永远不会发射。
编辑:从offer-get.php返回的表单
<tr>
<form action="offer-update.php" id="updateoffer" name="updateoffer" method='post'>
<input type="hidden" name="pendingofferid" value="<?php echo $record['pendingofferid'] ?>">
<input type="hidden" name="offerid" value="<?php echo $record['offerid'] ?>">
<input type="hidden" name="oldpaused" value="<?php echo $record['paused'] ?>">
<input type="hidden" name="oldpayout" value="<?php echo $record['payout'] ?>">
<td><?php echo $record['offerid']; ?></td>
<td><img src="<?php echo $record['picture']; ?>" height="50" width="50"></td>
<td><?php echo $record['name']; ?></td>
<td><?php echo $record['trackinglink']; ?></td>
<td><?php echo $record['country']; ?></td>
<td><?php echo $record['device']; ?></td>
<td>
<?php if ($approved) : ?>
<input type="number" name="payout" min="0" step="0.01" value="<?php echo $record['payout']; ?>">
<?php else: echo $record['payout']; ?>
<?php endif; ?>
</td>
<td>
<?php if ($approved) : ?>
<select name="status" class="form-control required" >
<option value="Live" <?php if(!$paused) echo 'selected="selected"'; ?> >Live</option>
<option value="Paused" <?php if($paused) echo 'selected="selected"'; ?> >Paused</option>
</select>
<?php else: echo 'Pending'; ?>
<?php endif; ?>
</td>
<td>
<?php if ($approved) : ?>
<button type='submit' name='submit' id='submit' class='btn btn-userid'>Update</button>
<?php endif; ?>
</td>
</form>
</tr>
显然这不是整个代码,不想发布客户敏感信息,但这应该足够了。此部分针对数据库查询的每个结果进行循环。所以有多种形式具有相同的ID。如果我为每个表单使用不同的ID,我不确定如何将事件触发,否则因为在返回数据之前不知道行数。