簡體   English   中英

如何修改我的代碼以從彈出窗口收集表單輸入,然后使用JavaScript將其插入到原始頁面上光標位置的textarea中?

[英]How do I modify my code to collect form input from a popup and insert it into a textarea at cursor location on original page using JavaScript?

下面顯示的JavaScript將表單輸入插入到當前光標位置的文本區域。 textarea id = mbentry。 如果所有代碼都在同一頁面上,則此方法有效。

我想在page1.php上有一個超文本鏈接,以打開一個小的彈出窗口(例如page2.php),以便用戶可以在彈出窗口中輸入文本,關閉該窗口,並使該窗口的輸入出現在textarea(id = mbentry)。 (注意:我目前使用GreyBox創建彈出窗口。)

我該如何完成? (請參見下面的代碼)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Page 1</title>

    <script type="text/javascript">
    window.onload = function()
    {
            btn = document.getElementById("btnInsertText");
            myText = document.getElementById("myTextArea");
            title = document.getElementById("insTitle");
            url = document.getElementById("insUrl");
            ltext = document.getElementById("insLText");
            btn.onclick = function()
            {
                insertAtCursor(myText, title.value, url.value, ltext.value);
            }
    }

    function insertAtCursor(myField, title, url, ltext)
    { 
        //IE support 
        if (document.selection)
        { 
            myField.focus();
            sel = document.selection.createRange(); 
            sel.text = '<a href="'+url+'" title="'+title+'" target="_blank">'+ltext+'</a>'; 
        }

        //Mozilla/Firefox/Netscape 7+ support 
        else if (myField.selectionStart || myField.selectionStart == '0')
        {  
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd; 
            myField.value = myField.value.substring(0, startPos)+ '<a href="'+url+'" title="'+title+'" target="_blank">'+ltext+'</a>' + myField.value.substring(endPos, myField.value.length);
        }

        else
        { 
            myField.value += myValue; 
        } 
    }       
    </script>

</head>
<body>
url: <input type="text" id="insUrl" /><br />
title: <input type="text" id="insTitle" /><br />
linked text: <input type="text" id="insLText" /><br />
<input type="button" id="btnInsertText" value="Insert Link" /><br /><br />
<textarea id="myTextArea" rows="6" cols="50"></textarea>
</body>
</html>

從彈出窗口中,您可以調用window.parent.insertAtCursor(...)

謝謝您的幫助。

我無法使用window.parent或top.window(GreyBox方法)使其工作。 經過大量研究,我決定使用包含表單輸入元素的隱藏div。 單擊“插入URL”按鈕時,將顯示div。 因為div與textarea在同一頁上,所以我不需要傳遞信息。 回到父窗口了。 我認為使用輸入字段顯示和隱藏div現在就足夠了。 如果我使用GreyBox彈出窗口提出一個更好的方法,我將分享。 再次感謝!

暫無
暫無

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

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