jQuery AJAX提交表单
jQuery AJAX提交表单
我有一个名为orderproductForm
的表单和一个未定义数量的输入框。
我想用jQuery.get或ajax或类似的方法调用一个页面进行Ajax请求,并发送表单orderproductForm
中的所有输入。
我想做的一种方法是:
jQuery.get("myurl", {action : document.orderproductForm.action.value, cartproductid : document.orderproductForm.cartproductid.value, productid : document.orderproductForm.productid.value, ...
然而,我不确定所有表单输入的确切数量。是否有一种功能、函数或其他东西,可以发送所有表单输入?
admin 更改状态以发布 2023年5月25日
你可以使用Ajax Form Plugin中的ajaxForm/ajaxSubmit函数,或使用jQuery序列化函数。
AjaxForm:
$("#theForm").ajaxForm({url: 'server.php', type: 'post'})
或
$("#theForm").ajaxSubmit({url: 'server.php', type: 'post'})
当提交按钮被按下时,ajaxForm将发送数据。而ajaxSubmit则会立即发送。
序列化:
$.get('server.php?' + $('#theForm').serialize()) $.post('server.php', $('#theForm').serialize())
这是一个简单的参考:\n
// this is the id of the form $("#idForm").submit(function(e) { e.preventDefault(); // avoid to execute the actual submit of the form. var form = $(this); var actionUrl = form.attr('action'); $.ajax({ type: "POST", url: actionUrl, data: form.serialize(), // serializes the form's elements. success: function(data) { alert(data); // show response from the php script. } }); });