使用AJAX提交评论而不刷新页面不再起作用

时间:2012-06-18 18:43:52

标签: jquery form-submit

您好我有这个代码通过AJAX和文件php提交评论而不刷新页面。 习惯了,但是因为我改变了计算机来开发这个应用程序,所以我遇到了问题并且代码从未改变过。 现在它要么崩溃Chrome,要么提交而不会到达文件comment.php

这是JS代码

    <script src="/js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript"> 
    $("#formcomment").submit(function(){        
        ctask = this.task.value;
        cname = this.usrnm.value;
        cmessage = this.comment.value;
        submitter = this.submitter;
        cty = this.ty.value;
        celem = this.elem.value;
        cusrcd = this.usrcd.value;
        cusrcdauth = this.usrcdauth;
        val_recaptcha_challenge_field = this.recaptcha_challenge_field.value;
        val_recaptcha_response_field = this.recaptcha_response_field.value;

        if(cname=="" || cmessage=="") { $("#errAll").html('<p>Please don\'t leave the requierd fields.</p>');  }

        $.post("/comment.php", {task: ctask, usrnm: cname, comment: cmessage, ty: cty, elem: celem, recaptcha_challenge_field:val_recaptcha_challenge_field, recaptcha_response_field:val_recaptcha_response_field, usrcd: cusrcd, usrcdauth: cusrcdauth}, function(data){
            if(data=='0') { $("#errAll").html('<p>Please don\'t leave the requierd fields.</p>'); }
            else if (data=='1') {$("#errAll").html('<p>The reCAPTCHA wasn\'t entered correctly. Try it again.</p>'); }
            else if (data=='2') {$("#errAll").html('<p>Username must contain 6 to 20 characters.</p>'); }
            else { 
                submitter.value="Comment posted"; 
                submitter.disabled=true; 
                $('#listcom').prepend(data); 
                $("#errAll").html('');
                window.location.hash = "#comments";
            }
        });

        return false;
    });
   </script>

页面中的表格

   <form class="well" method="post" id="formcomment"> 
    <input type="hidden" id="ty" name="ty" value="stg" /> 
    <input type="hidden" id="elem" name="elem" value="13" /> 
    <input type="hidden" name="task" id="task" value="addComments" /> 
    <input type="hidden" name="usrcdauth" id="usrcdauth" value="yudtjnisifwndyubleedbtisngvxlj" /> 

    <label>Username</label> 
    <input value="" type="text" id="usrnm" name="usrnm" maxlength="20" class="span3" /> 
    <input type="hidden" id="usrcd" name="usrcd" value="" /> 

    <label>Message</label> 
    <textarea id="comment" name="comment" rows="5" cols="150" class="span7"></textarea> 


    <div style="margin: 15px 0;"><input type="submit" id="submitter" name="submitter" value="Add Comment" class="btn" /></div> 
    <div id="errAll"></div> 
   </form> 

1 个答案:

答案 0 :(得分:0)

这可能听起来很傻但是你在新电脑上安装了像xammp这样的php服务器吗?如果是这样,请确保文件位于同一目录中,并尝试在comment.php之前删除“/”。我没有详细介绍你的代码,但我认为因为它之前有用;情况可能就是这样。