为什么函数按此顺序执行?

时间:2014-07-01 23:18:55

标签: javascript jquery

代码:

function fillChooseCompInAddDeviceDiv(){
        $.ajax({
        url: "${pageContext.request.contextPath}/StoreServlet",
        type: "get",
        data: {ajaxId: "2"},
        dataType: "json",
        success: function(data) {
            $("table#choseComponentAddDeviceDiv").find("td").remove();
            for(var i = 0; i<data.length; i++) {
                $("table#choseComponentAddDeviceDiv").append("<tr>" + "<td>" + data[i].name + "</td>" + "<td>" + data[i].price + "</td>" + "<td>" + data[i].amount + "</td>" + "<td>" + data[i].description + "</td>" + "<td>" + data[i].categoryName + "</td>" + "<td>" + "<a href='" + data[i].link + "'>" + data[i].link + "</a>" + "</td>" + "<td>" + "<img src = '" + data[i].imagePath + "' />" + "</td>" + "<td>" + "<input type='checkbox' class='checkBoxCol' value='" + data[i].name + "'>" + "</td>" + "</tr>");
            }
            alert("ALERT 1");
        },
        error: function(status) {
            //Do something  
        }
        });
}

//any edit clicked in eny table
function anyEditOnCLick(buttonId, buttonVal){
window.editIsClicked = true;
$("input.nameInput").attr("readonly", true);

if (buttonId === "device"){
    $("#addDeviceDiv").css("visibility", "visible");
    window.location.hash = "#addDeviceDiv";

    fillChooseCompInAddDeviceDiv();
    $.ajax({
        url: "${pageContext.request.contextPath}/StoreServlet",
        type: "get",
        data: {ajaxId: "11", toEdit: buttonVal},
        dataType: "json",
        success: function(data) {
            // puni text area
            $("#addDeviceFieldName").val(data.name);
            $("#addDeviceFieldDescription").val(data.description);
            // cekira odgovarajuce komponente, jedan samo data se ocekuje, ne data[]
            //fillChooseCompInAddDeviceDiv();
            for (var i = 0; i<data.components.length; i++){
                $("table#choseComponentAddDeviceDiv td input").filter(function(){return this.value == data.components[i].name}).attr("checked", true);
            }
            alert("ALERT 2");

        },
        error: function(status) {
            //Do something  
        }
    });

    //name is unique - cannot be changed
    $("#addDeviceDiv input#addDeviceFieldName").attr("readonly", true);

} else if (buttonId === "component"){
    $("#addComponentDiv").css("visibility", "visible");
    window.location.hash = "#addComponentDiv";

    fillCategorySelection();
    $.ajax({
        url: "${pageContext.request.contextPath}/StoreServlet",
        type: "get",
        data: {ajaxId: "21", toEdit: buttonVal},
        dataType: "json",
        success: function(data) {
            $("#addComponentFieldName").val(data.name);
            $("#addComponentFieldPrice").val(data.price);
            $("#addComponentFieldAmount").val(data.amount);
            $("#addComponentFieldDescription").val(data.description);
            $("#addComponentFieldLink").val(data.link);
            $("#form#uploadForm #imageFile").attr("value", data.imagePath);
            //fillCategorySelection();            
            $("#addComponentDiv select.categorySelect option").filter(function(){return this.value == data.categoryName}).attr("selected", "selected");

        },
        error: function(status) {
            //Do something  
        }
    });

    //name is unique - cannot be changed
    $("#addComponentDiv input#addComponentFieldName").attr("readonly", true);

} else if (buttonId === "category"){
    $("#addCategoryDiv").css("visibility", "visible");
    window.location.hash = "#addCategoryDiv";

    fillChoseSubCategoriesinAddCategoriesDiv();
    $.ajax({
        url: "${pageContext.request.contextPath}/StoreServlet",
        type: "get",
        data: {ajaxId: "31", toEdit: buttonVal},
        dataType: "json",
        success: function(data) {
            $("#addCategoryFieldName").val(data.name);
            $("#addCategoryFieldDescription").val(data.description);
            //fillChoseSubCategoriesinAddCategoriesDiv();
            for (var i = 0; i<data.subCategories.length; i++){
                $("table#choseSubCategoriesinAddCategoriesDiv td input").filter(function(){return this.value == data.subCategories[i].name}).attr("checked", true);
            }
        },
        error: function(status) {
            //Do something  
        }
    });

    //name is unique - cannot be changed
    $("#addCategoryDiv input#addCategoryFieldName").attr("readonly", true);
}
}

函数anyEditOnCLick(arg1,arg2)是事件函数,放在按钮的onClick标记中。 当我点击此按钮时,浏览器总是首先显示ALERT 2消息,然后执行fillChooseCompInAddDeviceDiv()并显示警告1.有人可以解释这里发生了什么,我怎样才能“说服”javascript首先执行{{1} }?在此之前,我认为函数调用在恢复执行外部代码之前提供执行整个函数体。

0 个答案:

没有答案