[英]Failure when trying to use serialize on argument from onSubmit='return function(this)'
New to javascript here, so sorry if this is elementary... 这里是javascript的新功能,如果这是基本功能,请抱歉。
So I've got a form, and I want to do some AJAX with it upon confirmation (confirm box after submit). 所以我有一个表格,我想在确认后用它做一些AJAX(提交后确认框)。 The outermost layer of the problem is that the serialize() isn't working--it returns a blank string. 问题的最外层是serialize()无法正常工作-它返回一个空字符串。 Obviously, I suspect there is more under the surface as to why it isn't working… 显然,我怀疑它为什么不起作用还有很多其他原因……
My goal here is to have the form go to the onsubmit method (confirm_delete()), upon which some AJAX is done (when user confirms desire to delete). 我的目标是让表单转到onsubmit方法(confirm_delete()),在此方法上完成一些AJAX(当用户确认要删除时)。 Either way, I don't want the form submitting and loading a new page--hence the 'return false;''s--I want the AJAX to take over. 无论哪种方式,我都不希望表单提交和加载新页面-因此'return false;'s-我希望AJAX接管。
So…this is the form: 所以……这是表格:
<form class='delete_item' method='post' onSubmit='return confirm_delete(this)'>
//lots of inputs here. . . .
<button type='submit' class='btn-xs btn-danger remove_submit'>X</button>
</form>
And here's my javascript/AJAX attempt… 这是我的javascript / AJAX尝试...
function confirm_delete(form){
var r = confirm("Are you sure you want to delete?");
var data = form.serialize();
// var x = 9;
if (r == true) {
$.ajax({
type: 'post',
data: data,
datatype: 'json',
success: function(response){
alert('hi');
//do stuff….
}
});
return false;
} else {
return false;
}
}
Thanks. 谢谢。
.serialize
is a jQuery method, not a native JavaScript
method, so you need to utilize the jQuery
wrapper ( $()
) to use it .serialize
是一个jQuery方法,而不是本机JavaScript
方法,因此您需要利用jQuery
包装器( $()
)来使用它。
//..
function confirm_delete(form){
var r = confirm("Are you sure you want to delete?");
//note the $(form).serialize() vs form.serialize()
var data = $(form).serialize();
//rest of your code...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.