weixin_33724659 2012-12-30 04:02 采纳率: 0%
浏览 22

jQuery ajax没有成功

I have a piece of HTML code:

<div id="question_form">
<form method="POST" id="question_form" action="addQuestion.php">
<input type="hidden" id="u_id" name="u_id" value="6"/>
<input type="hidden" id="u_a_name" name="u_a_name" value=""/>
<textarea placeholder="question here" cols="138" rows="3" name="question" id="question_input"/>
<input type="image" class="ask_button" value="submit" src="img/ask.png"/>
</form>
</div>

And here is JavaScript:

$(".ask_button").click(function() {
        $.ajax({
               type: "POST",
               url: "addQuestion.php",
               data: $("div#question_form").formSerialize() + "&textareadata="+escape($("#question_input").value),
               success: function(data)
               {
                        if (data == "Asked") {
                        window.location.reload();
                    } else {
                        alert(data);
                    }
               }
             });
        return false;
    });

The php file checks is question is empty and if not - writes it into database.

When I click on submit button I see the addQuestion.php page, not alert, though php script is executed correctly. What can be a problem?

  • 写回答

5条回答 默认 最新

  • Lotus@ 2012-12-30 04:21
    关注

    escape($("#question_input").value) should be encodeURIComponent($("#question_input").val())

    The jQuery object has no value property(use .val() instead) which is causing an error in your code and presumably causing the form to submit to addQuestion.php


    You also have two id tags with the same name:

    <div id="question_form">
    <form method="POST" id="question_form" action="addQuestion.php">
    

    Each id tag must be completely unique in the HTML for jQuery to supply expected results. Try renaming one of them.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R