如何将div作为参数传递给jQuery对话框

时间:2015-02-03 13:28:59

标签: jquery jquery-ui dialog jquery-ui-dialog

有没有办法将整个div作为参数传递给jQuery对话框?

例如,下面的代码显示了当我在对话框上单击“确定”时会发生什么,我已经将数据属性传递给对话框,我可以使用它来获取类的一些元素值。然后我将这些作为参数传递给我的ajax函数......

我想传递整个div而不是通过类获取其中的每个元素,因为这个当前函数是执行一些添加例程,我想使用相同的ajax例程执行更新,所以如果我可以将div作为参数传递,我可以在我的ajax函数中获取其内容。

Click功能:

$(".btnAddCommunity").click(function () {
    var emptyGuid = "00000000-0000-0000-0000-000000000000";
    $(".dvCommunityDialog").data("communityId", emptyGuid).dialog("open");
});

对话框:

$(".dvCommunityDialog").dialog({
    autoOpen: false,
    height: 'auto',
    title: "Interests and Online Community",
    width: 600,
    modal: true,
    resizable: false,
    buttons: {
        OK: function () {
            var communityName = $(".txtCommunityName").val();
            var communityDescription = $(".txtCommunityDescription").val();
            var communityLink = $(".txtCommunityLink").val();
            var communityId = $(".dvCommunityDialog").data("communityId");
            var dvOnlineCommunityMain = $("#dvOnlineCommunityMain");
            dvOnlineCommunityMain.append("<p>Test</p>");
            UpdateOnlineCommunity(communityId, communityName, communityDescription, communityLink);
            $(this).dialog("close");
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    },
    close: function () {
        $(this).dialog("close");
    }
});

Ajax请求功能:

function UpdateOnlineCommunity(communityId, communityName, communityDescription, communityLink) {
var serviceData = JSON.stringify({
    'communityId': communityId,
    'communityName': communityName,
    'communityDescription': communityDescription,
    'communityLink': communityLink
});
$.ajax({
    type: "POST",
    url: "PresentationService.asmx/UpdateUserCommunity",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: serviceData,
    success: function(response) {
        var data = response.d;

        var dvOnlineCommunityMain = $(".dvOnlineCommunityMain");

        var dvOnlineCommunity = $('<div/>', {
            id: "dvOnlineCommunity",
            class: "dvOnlineCommunity",
            "data-id": data.CommunityID
        });

        var lblOnlineCommunityDescription = $('<span/>', {
            id: "lblOnlineCommunityDescription",
            text: data.Description
        });

        var pOnlineCommunityDetails = $('<p/>', {
            id: "pOnlineCommunityDetails",
            text: data.Detail
        });

        var linkOnlineCommunity = $('<a/>', {
            id : "linkOnlineCommunity",
            href : data.Link,
            text: data.Link
        });

        var paragraph = $('<p></p>');

        var btnUpdateOnlineCommunity = $('<button/>', {
            id: "btnUpdateOnlineCommunity",
            class: "btnUpdateOnlineCommunity",
            text: "Update"
        });

        var btnDeleteOnlineCommunity = $('<button/>', {
            id: "btnDeleteOnlineCommunity",
            class: "btnUpdateOnlineCommunity",
            text: "Delete"
        });


        dvOnlineCommunity.append(lblOnlineCommunityDescription, pOnlineCommunityDetails, linkOnlineCommunity,
            btnUpdateOnlineCommunity, btnDeleteOnlineCommunity);

        dvOnlineCommunityMain.append(dvOnlineCommunity);
    },
    error: function(response) {
        alert(response);
    }
});
}

0 个答案:

没有答案