简体   繁体   English

jQuery防止在提交时重新加载表格

[英]jquery prevent reload form on submit

I want to upload files using javascript/jquery/ajax and in my case, I have to prevent the reload on form submitting on upload. 我想使用javascript / jquery / ajax上传文件,在我的情况下,我必须防止重新加载表单并在上传时提交。 Also, I want to add a delete button, such that, when file is uploaded, the delete button can delete this and same is the scenario, that to prevent the page refresh/reload on form submitting. 另外,我想添加一个删除按钮,以便在上传文件时,该删除按钮可以将其删除,这与防止在提交表单时刷新页面/重新加载页面的情况相同。

Hence, I've three buttons in a single form, UPLOAD , DELETE , NEXT and in my case, only next allows to submit the form by refreshing/action of form. 因此,在单个表单中有三个按钮UPLOADDELETENEXT ,在我的情况下,只有next按钮允许通过刷新/动作来提交表单。 And obviously, when file is just selected and the user directly hits the next button, it first uploads and then take his action. 显然,当仅选择文件并且用户直接单击下一个按钮时,它会先上传然后采取行动。

HTML: HTML:

    <form name="myform" method="post" id="FORM2" enctype="multipart/form-data">
// OTHER VALUES OF FORM
                    <input type="file" name="FileUpload-1" id="FileUpload-1"  class="file_upload" />

                    <input type="submit" value="Upload" id="upload" class="submitUpload" />
                    <input type="submit" value="Delete" id="delete" class="submitDelete" />


<input type="submit" name="" id="FORMSUBMIT">
</form>

JS: JS:

  $(".submitUpload").click(function(){ console.log('UPLOAD');
        action = "upload.php"; 
         $("#FORM").on('submit',function(){
          var options={
           url     : action,
           success : onsuccess
          };
        $(this).ajaxSubmit(options);
         return false;
         });
     }
     return false;

    });

     $(".submitDelete").click(function(){ console.log('DELETE');
        return false;
    });
 $('.FORMSUBMIT').click(function () {
         //CUSTOM HANDLING
      });

To Directly answer your question: 直接回答您的问题:

$("#FORM").on('submit',function(evt){
    evt.preventDefault();
});

This piece of code will prevent the normal form submitting. 这段代码将阻止正常表单提交。 My advice is to transform the Update and Delete buttons from type=submit to type=button. 我的建议是将“更新”和“删除”按钮从type = submit转换为type = button。 Like this: 像这样:

<input type="button" value="Delete" id="Delete" class="submitDelete" />

This will prevent the form to be submitted when delete or update are pressed... 这将防止在按下删除或更新时提交表单...

I wrote a jsfiddle for you: https://jsfiddle.net/yL35bh10/ 我为您写了一个jsfiddle: https ://jsfiddle.net/yL35bh10/

I recently prototyped a page needing image file upload. 我最近制作了一个页面原型,需要上传图像文件。 I found a package on GitHub that was easy to use and should provide the majority of the functionality you're needing. 我在GitHub上找到了一个易于使用的软件包,该软件包应提供您所需的大多数功能。 https://github.com/blueimp/jQuery-File-Upload https://github.com/blueimp/jQuery-File-Upload

My only question to you: Once a file is uploaded to the server, how are you going to know what to delete? 我唯一的问题是:文件上传到服务器后,如何知道要删除的内容?

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

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