简体   繁体   English

jQuery Form Plugin不按AJAX提交表单,它仍在重新加载页面,为什么?

[英]jQuery Form Plugin doesn't submit forms per AJAX, it's still reloading the page, why?

Can't seem to get this running, I am using jQuery 3.2.1 together with the jQuery Form Plugin 4.2.1 . 似乎无法正常运行,我正在使用jQuery 3.2.1jQuery Form Plugin 4.2.1 I also have included the jQuery Cookies Plugin 1.4.1 . 我还包括了jQuery Cookies Plugin 1.4.1 Now, if I submit a Form, that is called by .ajaxForm(); 现在,如果我提交一个表单,则由.ajaxForm()调用; it doesn't work and still reloads the page and redirects me to the action-path that is included in the form. 它不起作用,仍然会重新加载页面,并将我重定向到表单中包含的操作路径。 I tried it with a lower version of jQuery, 1.2 and with that it worked fine. 我使用较低版本的jQuery 1.2进行了尝试,并且运行良好。 But if I use the older version, some other functions wouldn't work anymore, what's why I need to use the latest one. 但是,如果我使用的是旧版本,则某些其他功能将无法使用,这就是为什么我需要使用最新版本的原因。

Here's my head-tag: 这是我的头像:

    <!--- JQUERY --->
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

    <!--- JQUERY FORM PLUGIN --->
    <script src="js/de.jqu.form.421.js"></script>

    <!--- JQUERY COOKIE PLUGIN --->
    <script src="js/de.jqu.cookies.141.js"></script>

    <!--- JQUERY MIGRATE --->
    <script src="https://code.jquery.com/jquery-migrate-3.0.0.js">

The HTML form: HTML形式:

<form enctype="multipart/form-data" name='imageform' role="form" id="imageform" method="post" action="/functions/c_uploadcans.php">

                    ...

    <input type="submit" value="Upload" name="image_upload" id="image_upload" class="btn"/>
</form>

And the script I am using to call the .ajaxForm(): 还有我用来调用.ajaxForm()的脚本:

(function() {
    $('#imageform').ajaxForm({
        beforeSubmit: function() {  
            count = 0;
            val = $.trim( $('#images').val() );

            if( val == '' ){
                count= 1;
                $( "#images" ).next('span').html( "Please select your images" );
            }

            if(count == 0){
                for (var i = 0; i < $('#images').get(0).files.length; ++i) {
                    img = $('#images').get(0).files[i].name;
                    var extension = img.split('.').pop().toUpperCase();
                    if(extension!="PNG" && extension!="JPG" && extension!="GIF" && extension!="JPEG"){
                        count= count+ 1
                    }
                }
                if( count> 0) $( "#images" ).next('span').html( "Please select valid images" );
            }


            if( count> 0){
                return false;
            } else {
                $( "#images" ).next('span').html( "" );
            }



        },

        beforeSend:function(){
           $('#loader').show();
           $('#image_upload').hide();
        },
        success: function(msg) {
        },
        complete: function(xhr) {
            $('#loader').hide();
            $('#image_upload').show();

            $('#images').val('');
            $('#error_div').html('');
            result = xhr.responseText;
            result = $.parseJSON(result);
            base_path = $('#base_path').val();

            if( result.success ){
                name = base_path+'images/'+result.success;
                html = '';
                html+= '<image src="'+name+'">';
                $('#uploaded_images #success_div').append( html );
            } else if( result.error ){
                error = result.error
                html = '';
                html+='<p>'+error+'</p>';
                $('#uploaded_images #error_div').append( html );
            }


            $('#error_div').delay(5000).fadeOut('slow');


        }
    }); 

})(jQuery);

Maybe someone finds a mistake, I really have no idea. 也许有人发现错误,我真的不知道。 Thanks in advance. 提前致谢。

how are you calling that function. 您如何调用该函数。 may be its not even called. 可能甚至没有被调用。 put that in ` 把它放在`

$(document).ready(function()`{ your ajaxForm code }); $(document).ready(function()`{您的ajaxForm代码});

like this ... 像这样 ...

$(document).ready(function() {
    $('#imageform').ajaxForm({
        beforeSubmit: function() {  
            count = 0;
            val = $.trim( $('#images').val() );

         ......
         ......
    });
 });

hope this works for you 希望这对你有用

My guess is because in your beforeSend all you are doing is showing the loader and hiding image_upload 我的猜测是因为在你beforeSend所有你正在做的是展示loader和隐藏image_upload

beforeSend:function(){
    $('#loader').show();
    $('#image_upload').hide();
},

Shouldn't you be handling ajax post call here as well? 您不应该在这里也处理ajax帖子吗?

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

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