简体   繁体   English

尝试从onSubmit ='return function(this)'使用参数序列化时失败

[英]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.

相关问题 形式:onsubmit不从函数获取返回值 - Form: onsubmit not taking return value from a function 为什么在调用OnSubmit时必​​须返回函数的返回值? - Why I have to return the return of my function when calling OnSubmit? Javascript 尝试在 function 的参数中使用变量时出现语法错误 - Javascript syntax error when trying to use a variable in the argument of a function 从onsubmit返回false时,HTML表单仍在提交吗? - HTML form still submit when I return false from onsubmit? 我正在尝试通过使用 onsubmit=return validateForm() 在我的 html 标记中使用此验证 - I am trying to use use this validation in my html tag by using onsubmit=return validateForm() 当我尝试在我的 onsubmit function 中调用 function 时,未定义 function - function is not defined when im trying to call a function inside my onsubmit function 提交功能不会触发onsubmit返回功能吗? - submit function does not trigger onsubmit return function? 当 onsubmit 事件仅以第一个 function 工作时,我想返回所有 3 个 function 为 true - when onsubmit event in form only first function is working i want to return all 3 function with true 从提交 function 触发 onSubmit - Trigger onSubmit from submit function 尝试从递归函数返回值时获取“未定义” - Getting 'undefined' when trying to return a value from a recursive function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM