图片上传 JQuery 表单和 PHP 停止跳转页面



我正在处理图像上传页面。上传有效,但每当我点击提交时,它都会转到 php 页面。我返回了假的并设置了预防。有没有办法阻止它进入php页面?

我的网页代码

 <form action="imageupload.php" method="post" id="image_form"enctype="multipart/form-data">
     <input id="update_image" name="file" type="file" maxlength="40">
     <input id ="image_submit" type="submit" name="submit" value="Submit">
 </form>
<div id="output"> </div>

我的 js 代码

var options = { 
        target:   "#output",   // target element(s) to be updated with server response 
       // beforeSubmit:  beforeSubmit,  // pre-submit callback 
        resetForm: true        // reset the form after successful submit 
    }; 
 $("#image_form").submit(function(e) { 
        $(this).ajaxSubmit(options);  //Ajax Submit form            
        // return false to prevent standard browser submit and page navigation
        e.preventDefault();
        return false; 
    }); 

试试这个

<form action="javascript: your function name" method="post" id="image_form" enctype="multipart/form-data">

或尝试替换 预防默认

 $("#image_form").submit(function(event) { 
       event.preventDefault();
       $(this).ajaxSubmit(options);  //Ajax Submit form                               
       return false; 
       }); 
你可以

提交它,而无需通过 FromData 对象和 ajax 调用重新加载页面。这将使您可以在不重新加载页面的情况下提交图像或文件,因此您可以根据需要忘记html表单标签以及提交阻止默认值。

我的博客中有一个例子:

http://sergiofraile.azurewebsites.net/?p=621

抱歉,它是西班牙语,但代码可能很有用。

最新更新