Reputation: 222
I have JavaScript for a commenting system, however when I click on the submit button with the class name "com_submit" nothing happens except the page reloads. Even if I leave the form empty and submit the alert should pop up but it isn't. What am I doing wrong?
Here is my code:
$(function() {
$('.com_submit').live("click", function() {
var comment = $("#comment").val();
var user_id = $("#user_id").val();
var perma_id = $("#perma_id").val();
var dataString = 'comment='+ comment + '&user_id='+ user_id + '&perma_id=' + $perma_id;
if(comment=='') {
alert('Please Give Valid Details');
}
else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:first").fadeIn("slow");
$("#flash").hide();
}
});
}
return false;
});
});
I have tried using .click, .live and .bind none of these work
Upvotes: 0
Views: 299
Reputation: 69905
There is a typo in your code because of which runtime error occurs and page reloads since it is a link.
var perma_id = $("#perma_id").val();
$(function() {
$('.com_submit').live("click", function() {
var comment = $("#comment").val();
var user_id = $("#user_id").val();
var perma_id = $("#perma_id").val();
var dataString = 'comment='+ comment + '&user_id='+ user_id + '&perma_id='
+ perma_id;//<<<------ Here was the typo(You have used $perma_id)
if(comment=='') {
alert('Please Give Valid Details');
}
else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:first").fadeIn("slow");
$("#flash").hide();
}
});
}
return false;
});
});
Upvotes: 2
Reputation: 207861
That's working as expected. In other words, your submit button is submitting. What you want to do is stop the normal behavior. Try this:
$('.com_submit').live("click", function(e) {
e.preventDefault();
.
.
.
At the top of your code.
Upvotes: 0