jQuery.ajax() not firing
I used Firebug to debug my code. In the "Net" panel, I don't see the .ajax()
firing at all. (didn't see any outgoing request url)
What is wrong with my code?
Edited: After i added $(document).ready(). to my co开发者_如何学Cde, everything just works fine. Why do I need $(document).ready() ? I thought when the button is clicked, the document is ready for sure. Needed to add "return false;" as well
function doSubmit() {
alert('button is clicked.');
$.ajax({
type: 'Post',
url: "http://mysite.com/list/json",
dataType: "json",
context: [],
success: function(data){
alert('got data');
}
});
}
<form name="my_form" id="myform">
<div class="mydiv">
<input class="button" type="submit" value="Save" onClick="doSubmit();">
</div>
</form>
You have to return false, otherwise it just submits that form
function doSubmit() {
alert('button is clicked.');
$.ajax({
type: 'Post',
url: "http://mysite.com/list/json",
dataType: "json",
context: [],
success: function(data){
alert('got data');
}
});
return false;
}
$('#myform').submit(function() {
$.ajax({
// ...
});
});
精彩评论