如何停止jQuery帖子页面刷新 [英] How to stop jQuery post page refresh

查看:172
本文介绍了如何停止jQuery帖子页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP从MySQL数据库获取数据.我正在使用jQuery从发送并从PHP获取数据.这是代码.

I am getting data from an MySQL database through PHP. I am sending the and getting the data from PHP using jQuery. Here is the code.

$.POST("SubmitCode.php", $("#questionCodeForm").serialize(),'json').done(function(data) {});

现在的问题是,一旦我发送此数据,页面就会刷新.如何停止页面刷新.如果我删除"json",则页面将停止刷新,但问题是我想获取不刷新页面的json数据.我该怎么办?

Now the problem is that once I send this data the page refreshes. How can I stop the page refresh. If I delete 'json' then the page stops refreshing but the problem is that I want to get the json data without page refresh. How can I do this?

-------------------------------------------编辑后- -------------------------------------------------- ------------------------- 这是更新的代码

-------------------------------------------after edit----------------------------------------------------------------------------- Here is the updated code

$(document).ready(function() {
    initialization();

    codeSubmission();
});

function initialization() {
    $("#answerForm").hide();
}

function codeSubmission() {
  $("#submitButton").click(function(e) {
    e.preventDefault();
    $.post("SubmitCode.php", $("#questionCodeForm").serialize()).done(function(data) {
        var questionName = data.questionName,
            options = data.options,
            pollingStatus = data.pollingStatus,
            codeExist = data.codeExist;

        alert(data);
        alert(data[1]);
        alert(questionName);
        alert(options);

        if(codeExist == true) {
            $("#questionTitle").text("questionName");

            for(rowNum=1;rowNum<=5;rowNum++) {
                $("#checkbox-"+rowNum).val("Answer1");
                $("#checkbox"+rowNum+"label").text("Answer"+rowNum);
            } 

            $("#answerForm").slideDown(500);

        } else if(codeExist == false) {
            alert("This quiz code is invalid");
        }   
    },'json');  
    //return false;
  });
    //return false;
}

现在的问题是,alert(questionName)的输出未定义.数据作为字符串传递.如何在正确的变量中获取正确的信息?

Now the problem is that the output of alert(questionName) is undefined. The data is passed as a string. How do I get the correct information in the correct variables?

推荐答案

您还必须在客户端解析json.您可以使用

You also have to parse the json on the client side. You can do this using

obj = jQuery.parseJSON(data);

obj = jQuery.parseJSON(data);

这篇关于如何停止jQuery帖子页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆