0

An building a jquery mobile which contains a file upload page. its a multiple template app. When a user uploads a file i want the user to be redirected to a new page without refreshing the div. With my current script when the upload is done the address bar refreshes. I used event.preventDefault(); which i taught could stop the page from refreshing after sending the data but still does refresh

<script language="javascript">

$(document).ready(function(){
$("#form1").on('submit',function(event){
event.preventDefault();


$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST",             // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false,       // The content type used when sending data to the server.
cache: false,             // To unable request pages to be cached
processData:false,        // To send DOMDocument or non processed data file it is set to false
success: function(data)  {
$('#msg').html(data);

var result = $.trim(data);

if(result==="OK"){
$.mobile.changePage("#page2");

    }
  }
});
});
});

</script>
i_user
  • 511
  • 1
  • 4
  • 21

2 Answers2

1

Try this

$('#form1').live('submit' ,function(event){
    event.preventDefault();      
    // the rest
});

or

$("#form1").submit(function(e){
    //the rest
    return false;
});
Klezper
  • 49
  • 6
  • You can see the difference here: http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – Klezper Feb 17 '15 at 19:46
0

You can also use stopPropagation and also return false.

Thanasis Pap
  • 2,031
  • 2
  • 17
  • 19