簡體   English   中英

使用jQuery在父窗口和子彈出窗口之間傳遞數據

[英]Passing data between a parent window and a child popup window with jQuery

我有以下HTML

<tr>
    <td class="label" valign="top">
        Affiliate Party
    </td>
    <td class="field">
        <input type="hidden" name="ctl00$MainContent$ExternalAccountAttributes$AffiliatePartyId" id="AffiliatePartyId" />
        <input name="ctl00$MainContent$ExternalAccountAttributes$AffiliatePartyName" type="text" id="AffiliatePartyName" class="PartyLookup" />
    </td>
</tr>

和以下Javascript / jQuery

$(".PartyLookup").after("<img src='Images/book_open.png' class='PartyLookupToggle' style='padding-left:4px;' />");

$(".PartyLookupToggle").click(function () {
    window.open("PartySearch.aspx", "PartySearch", "width=400,height=50");
    return false;
});

我需要能夠使用class =“PartyLookup”標記任何PartyId輸入字段,以便它將修改DOM並將圖像包含在輸入字段旁邊。 彈出窗口返回數據以填充隱藏字段和文本字段,但由於click()是通用的,我需要傳遞輸入字段的ID。 我不知道該怎么做。 有什么建議?

父頁面上的腳本:

$(".PartyLookupToggle").click(function () {
    var id = $(this).prev().prev().attr("id");
    var name = $(this).prev().attr("id");

    var url = "PartySearch.aspx?id=" + id + "&name=" + name;

    window.open(url, "PartySearch", "width=400,height=50");
    return false;
});

子頁面上的腳本:

// Get the values from the URL using the jquery.query plug-in
var id = $.query.get("id");
var name = $.query.get("name");

// Get the values from the drop down
var newPartyId = $("#ddlMatchingParties").val();
var newPartyName = $("#ddlMatchingParties option:selected").text();

// Set them to the parent window
window.opener.$("#" + id).val(newPartyId);
window.opener.$("#" + name).val(newPartyName);

// Close the popup
window.close();

使用jQuery非常簡單,在子窗口(彈出窗口)中調用父窗口對象:

$("#txtCodCliente", opener.window.document).val("VALUE TO "); //assign

$("#btnSelCliente", opener.window.document).click();

使用opener.window.document我們告訴jQuery該對象在窗口中打開彈出窗口。

看看這篇教學文章: http//www.plus2net.com/javascript_tutorial/window-child3.php

基本上,您需要在子窗口的表單中執行此操作。 你將傳遞一個這樣的值:

opener.document.f1.p_name.value="Any value";

其中f1是父窗口中表單的ID, p_name是表單中字段的名稱。

獲得父級字段中的值后,您可以隨意執行任何操作。

編輯:

要將信息傳遞給子窗口,最簡單的方法可能是通過查詢字符串,然后從子窗口中讀取查詢字符串。 在這種情況下,可能是這樣的:

$(".PartyLookupToggle").click(function () {
    window.open("PartySearch.aspx?id=" + $(this).prev().attr('id'), "PartySearch", "width=400,height=50");
    return false;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM