我有产品列表页面,其中所有列出的产品都使用 while 循环使用不同的表单名称和 ID,如下面的代码段所述。我想在用户单击特定产品的“添加”按钮时这样做,然后应使用 AJAX/jQuery 提交该特定产品的表单。
产品列表.php
<script language="JavaScript" src="./product_listing.js"></script>
while loop to list products on page {
<form id="frm_add<?php print $rs_list->Fields("pkid"); ?>" name="frm_add<?php print $rs_list->Fields("pkid"); ?>" novalidate>
<input type="hidden" name="hdn_prodpkid" id="hdn_prodpkid" value="<?php print $rs_list->Fields("pkid"); ?>">
... other variables...
<button type="submit" name='btn_addtocart' id='btn_addtocart'>Add</button>
</form>
end of while loop }
我想要提交表单的 ID,以便我可以获取该提交表单的输入字段值。但是在每次提交表单时,我都会得到最后一个表单的 ID。
那么如何在 JS 页面上获取唯一(仅提交)表单的 ID?请指教。
当只有一个表单要提交时,下面的代码可以完美运行。在这种情况下,表单 ID 在 .php 和 .js 页面上都是预定义的。
product_listing.js
$(function() {
$("#"+NEED_SUBMITTED_FORM_ID_HERE+" input, #"+NEED_SUBMITTED_FORM_ID_HERE+" select").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
...other code...
},
submitSuccess: function($form, event) {
event.preventDefault();
var hdn_prodpkid = $("input#hdn_prodpkid").val();
... other variables...
$.ajax({
url: "./product_addtocart_p.php",
type: "POST",
data: {
hdn_prodpkid: hdn_prodpkid,
... other variables...
},
cache: false,
success: function(data)
{
...other code...
}
}
});
});
12345678_0001
慕田峪9158850
至尊宝的传说