简体   繁体   English

Ajax表单提交不起作用

[英]ajax form submit not working

I am using jquery.form.js (version: 2.84) and in the below code, I see that it's getting stuck just before $('imageform').ajaxSubmit.. If I remove preventDefault, the form gets submitted as usual and the page gets refreshed. 我正在使用jquery.form.js(版本:2.84),在下面的代码中,我看到它在$('imageform')。ajaxSubmit之前卡住了。如果删除了preventDefault,则该表单将照常提交,并且页面刷新。 When preventDefault is present, I do see the loader.gif, but in the server side I do not see any data and also the alert is never fired.. 当preventDefault存在时,我会看到loader.gif,但是在服务器端我看不到任何数据,并且永远不会触发警报。

google.maps.event.addDomListener($('photoimg'), 'change', function(e){
            e.preventDefault(); // <-- important
            $("preview").innerHTML = ("<img src=\"loader.gif\" alt=\"Uploading....\"></img>");
            $('imageform').ajaxSubmit({
                target: 'preview'
            });
            alert('after submit');
        });

I am also using a function as below and that's why I am using $('imageform') and not $('#imageform')... 我也使用如下函数,这就是为什么我使用$('imageform')而不是$('#imageform')...

function $(element) {
  return document.getElementById(element);
}

No, because you've overwritten $ with the getElementById function. 不,因为您已经用getElementById函数覆盖了$

$('imageform').ajaxSubmit

will execute ajaxSubmit on the DOM element, where you need a jQuery object. 将在需要jQuery对象的DOM元素上执行ajaxSubmit You'd either need to remove your $ or use: 您需要删除$或使用:

jQuery('#imageform').ajaxSubmit

$("#foo") is not the same as document.getElementById("foo") . $("#foo")document.getElementById("foo")

  • The jQuery function returns a jQuery object which has all the jQuery functions like .bind and .ajaxSubmit present. jQuery函数返回一个jQuery对象,其中包含所有jQuery函数,例如.bind.ajaxSubmit

  • document.getElementById , on the other hand, returns a naked DOM element which does not have any functions of jQuery. 另一方面, document.getElementById返回没有jQuery任何功能的裸DOM元素。

The jQuery object is like an array of DOM elements, so: jQuery对象就像是DOM元素数组,因此:

$("#foo")[0] === document.getElementById("foo")

Your $ function is clobbering the meaning of that same variable for jQuery. 您的$函数正在破坏jQuery相同变量的含义。 You could mess around with lots of compatibility stuff in jQuery, or take the simple route: rename your $ function to something else, or just call document.getElementById() straight out since your function is only saving you from typing out that call. 您可能会在jQuery中遇到很多兼容性问题,或者采取简单的方法:将$函数重命名$其他名称,或者直接调用document.getElementById() ,因为您的函数只会使您不必键入该调用。 Or, better yet, use jQuery to take care of that call. 或者,更好的是,使用jQuery来处理该调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM