提交表格而不重新登记页面

时间:2015-06-22 15:01:58

标签: java jquery ajax jsp

我有以下代码:

(function myFunction() {
$('form#uploadform').on('submit', function(e) {
    $.post('UploadServlet', $(this).serialize(), function (data) {
        // This is executed when the call to mail.php was succesful.
        // 'data' contains the response from the request
    }).error(function() {
        // This is executed when the call to mail.php failed.
    });
    e.preventDefault();

});

return false; 
});
</script>

   <form action="UploadServlet" method="post" id="uploadform"  
                    enctype="multipart/form-data">
            <input type="file" name="picture" size="50" placeholder="Browse Image" />
              <br />
           <button onclick="myFunction()">Submit</button>
           </form> 

我有一个上传文件功能,我想上传文件,但想留在同一页面,我尝试了上面的功能,但它没有用,我被重定向到另一个页面?

1 个答案:

答案 0 :(得分:0)

submit上运行的函数需要返回false,这样才能通过浏览器正常提交表单。