[英]jQuery Serialize() submitting a form
I have a form that gets submitted when I click the button and it sends an email and everything works good. 我有一个表单,当我单击按钮时会提交该表单,它会发送一封电子邮件,并且一切正常。
I want to change the form to use the type="submit"
and when i do and change my function it never sends the email. 我想更改表单以使用type="submit"
并且当我更改函数时,它永远不会发送电子邮件。
What should I do to make this possible. 我应该怎么做才能做到这一点。
<!-- Changed to type="submit" for browser validation" -->
<button type="button" class="btn btn-primary" id="email-button" name="submit">
Submit Emails
</button>
All this works for me 这一切对我有用
$('#email-button').on("click", function(){
if(TestInput())
{
PostCustomerOrderData();
}
});
PostCustomerOrderData() PostCustomerOrderData()
function PostCustomerOrderData() {
$.ajax({
type: "POST",
url: "./send-email.php",
data: $("form").serialize()
})
.done(function(data) {
var json = JSON.parse(data);
//console.log(json);
if(json.error == 1)
{
//Something wrong has happened
alert(json.error_message);
}
else if(json.error == 0)
{
alert("Your order has been sent successfully");
// Clear all fields
ClearFormElements();
if(PROCESS_SAVE_ORDERS)
{
var orders = localStore.getValue(ORDERS_KEY);
orders.pop(); //Remove current order from the orders (in local storage)
localStore.setValue(ORDERS_KEY, orders);
/* Added in to make pending button alter
the total number of orders pending for user feedback */
RenderPendingBtn();
// run function again
FetchOrderFromLocalStorage();
}
}
})
.fail(function (){
var form = $('form').serializeArray();
//console.log(form);
if(!PROCESS_SAVE_ORDERS)
{
var orders = localStore.getValue(ORDERS_KEY);
if(orders == null)
{
orders = [];
}
orders.push(form);
localStore.setValue(ORDERS_KEY, orders);
ClearFormElements();
}
});
}
I have changed the button type to submit, and the on("submit")
for the jQuery function but I am lost now. 我已经更改了按钮类型以提交,并且将jQuery函数的on("submit")
更改了,但是我现在迷路了。
I would guess that the default form action is overriding your jQuery event. 我猜想默认的表单操作会覆盖您的jQuery事件。
In your form tag add onsubmit="return false"
在您的表单标签中添加onsubmit="return false"
$("form").on("submit",function(e){
//all your code goes here
e.preventDefault();
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.