当前位置: 代码迷 >> Ajax >> jquery中在一个click事件中,ajax重复执行两次
  详细解决方案

jquery中在一个click事件中,ajax重复执行两次

热度:564   发布时间:2012-04-12 15:46:35.0
jquery中在一个click事件中,.ajax重复执行两次 - Web 开发 / Ajax
激发了一次$("#qdanswersubmit").click(submitAnswer)事件,可是在函数submitAnswer中,不知道为什么.ajax运行了两次。
js源代码如下:
JScript code

$(document).ready(function () {
    initControls();
    bindEvents();
});
function initControls() {
    $(".qduesfulmesg").hide();
    $("#qdanswersubmit").attr({ "disabled": "disabled" });
}
function bindEvents() {
    //submit the answer
    $("#qdanswersubmit").click(submitAnswer);
    $("#qdanswertext").keyup(checkContent);
}
function submitAnswer() {
    $.ajax({
        url: "SubmitAnswer.ashx",
        cache: false,
        type: "POST",
        dataType: "json",
        data: {
            content: $("#qdanswertext").val()
        },
        success: submitSuccess,
        error: ajaxError
    });
}
//post ajax occure an error
function ajaxError() {
    $.blockUI({ message: '<img src="../images/handle/Failed.png" /><h2 class="ajaxmsg">Ajax Request Failed!</h2>' });
    setTimeout($.unblockUI, 1500);
}
function submitSuccess(data) {
    var answer = "";
    var value = $("#qdanswerscount").text();
    if (data != null) {
        if (value != 0) {
            answer += "<hr style='margin-top: 3px; margin-bottom: 3px; width:80%' />";
        }
        answer += "<div class='qdansweruseful' style='display: block; float: left; color: #808185; text-align: center; font-size: 30px; margin-top: 5px; margin-left: 15px;'>";
        answer += "<img width='36px' height='28px' id='qdanswer_useful" + data.aid;
        answer += "' src='../images/handle/arrow.png' title='This is your answer, you cann't click again to undo' alt='" + data.aid;
        answer += "' class='.qdcontent_useful' style='cursor: pointer' />"
        answer += "<div style='text-align: center'><label id='qdanswer_usefulcount" + data.aid + "' ";
        answer += "style='display: block; padding-top: 5px;'>";
        answer += "0</label></div></div>";
        answer += "<div class='qdcontent_main'><div class='qduesfulmesg' id='qduesfulmesg" + data.aid + "'>";
        answer += "<label>Is it useful for you?</label><br /><input class='qdanswer_addsubmit' id='qdanswer_adduseful" + data.aid + "' type='button' value='Yes' name='" + data.aid + "'/>";
        answer += "<input class='qd_addsubmit' id='qdanswer_canceluseful" + data.aid + "' type='button' value='No' /></div>";
        answer += "<span id='qdcontent_text'>" + $("#qdanswertext").val() + "</span><span class='qdcontent_tag'><label for='qdcontenttag' class='qdlabel'>Tags: </label><label id='qdcontenttag'>" + data.tag;
        answer += "</label></span>";
        answer += "<span class='qdcontent_tag'><label for='qdcontentdate' class='qdlabel'>Publishdate: </label><label id='qdcontentdate'>" + data.date;
        answer += "</label></span>";
        answer += "<span class='qdcontent_tag'><label for='qdcontentquestioner' class='qdlabel'>Responder: </label><label id='qdcontentquestioner'>" + data.uid;
        answer += "</label></span></div>";
        $(".qdmesgcontent").append(answer);
        value = parseInt(value) + 1;
        $("#qdanswerscount").text(value);
        $(".qduesfulmesg").hide();
        $("#qdanswertext").text("");
    } else {
        $.blockUI({ message: '<img src="../Images/handle/Failed.png" /><h2 class="ajaxmsg">Post failed, please sign in firstly!</h2>' });
        setTimeout($.unblockUI, 4500);
    }
    $("#qdanswersubmit").attr({ "disabled": "disabled" });
}

function checkContent() {
    var len = $("#qdanswertext").val().length;
    $("#qdanswererrormesg").empty();
    if (len == 0) {
        $("#qdanswererrormesg").append("Please enter the answer firstly.");
        $("#qdanswersubmit").attr({ "disabled": "disabled" });
    } else {
        $("#qdanswersubmit").removeAttr("disabled");
    }
}

 
  相关解决方案