2012-01-17 148 views
0

我对评论系统有JavaScript,但是当我点击类名为“com_submit”的提交按钮时,除了页面重新加载之外,没有任何事情发生。即使我将表单留空并提交应该弹出的警报,但事实并非如此。我究竟做错了什么?.click功能不起作用

这里是我的代码:

$(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; 
}); 
}); 

我一直在使用。点击试过,.live和.bind没有这些工作

+0

你还可以提供按钮声明吗? – Praveen 2012-01-17 19:30:05

+0

是的,按钮是:'' – Sygon 2012-01-17 19:30:47

回答

2

由于运行时错误和页面重新加载,因为它是一个链接,所以代码中存在拼写错误。

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; 
}); 
}); 
+0

谢谢,我不能相信我错过了。修复了问题 – Sygon 2012-01-17 19:34:21

+0

我很高兴它帮助你:) – ShankarSangoli 2012-01-17 19:37:31

0

这是工作正常的。换句话说,您的提交按钮正在提交。你想要做的就是停止正常的行为。试试这个:

$('.com_submit').live("click", function(e) { 
e.preventDefault(); 
. 
. 
. 

在代码的顶部。