最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pop-up for Editing Input Field - Stack Overflow

programmeradmin0浏览0评论

I am creating an HTML form. Inside it, there is one input box, for which I want to create a popup that provides a textarea. This would give the user more space to enter a larger block of text. My current code enables the child window (pop-up) to send the entered text back to the parent. However, I also need to have the parent window send the current text to the textarea in the child window. I'd appreciate some help.

I created a simplified version, with just one input box, for the purpose of this discussion.

Here is the code from the parent:

<HTML><HEAD></HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function openChild(file,window) {
    childWindow=open(file,window,'resizable=no,width=200,height=400');
    if (childWindow.opener == null) childWindow.opener = self;
    }
//--></SCRIPT>

<BODY>
<FORM NAME="parentForm">
<INPUT TYPE="button" VALUE="Open child" onClick="openChild('examplejs2.html','win2')">
<BR><INPUT NAME="pf1" TYPE="TEXT" VALUE="">
</FORM></BODY></HTML>

Here is the code from the child window (pop-up):

<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function updateParent() {
    opener.document.parentForm.pf1.value = document.childForm.cf1.value;
    opener.document.parentForm.pf2.value = document.childForm.cf2.value;
    if (document.childForm.cf3[0].checked)
       opener.document.parentForm.pf3[0].checked = true;
    if (document.childForm.cf3[1].checked)
       opener.document.parentForm.pf3[1].checked = true;       
    self.close();
    return false;
}
//--></SCRIPT>
</HEAD><BODY>
<FORM NAME="childForm" onSubmit="return updateParent();">
<BR><INPUT NAME="cf1" TYPE="TEXT" VALUE="">
</FORM></BODY></HTML>

I am creating an HTML form. Inside it, there is one input box, for which I want to create a popup that provides a textarea. This would give the user more space to enter a larger block of text. My current code enables the child window (pop-up) to send the entered text back to the parent. However, I also need to have the parent window send the current text to the textarea in the child window. I'd appreciate some help.

I created a simplified version, with just one input box, for the purpose of this discussion.

Here is the code from the parent:

<HTML><HEAD></HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function openChild(file,window) {
    childWindow=open(file,window,'resizable=no,width=200,height=400');
    if (childWindow.opener == null) childWindow.opener = self;
    }
//--></SCRIPT>

<BODY>
<FORM NAME="parentForm">
<INPUT TYPE="button" VALUE="Open child" onClick="openChild('examplejs2.html','win2')">
<BR><INPUT NAME="pf1" TYPE="TEXT" VALUE="">
</FORM></BODY></HTML>

Here is the code from the child window (pop-up):

<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function updateParent() {
    opener.document.parentForm.pf1.value = document.childForm.cf1.value;
    opener.document.parentForm.pf2.value = document.childForm.cf2.value;
    if (document.childForm.cf3[0].checked)
       opener.document.parentForm.pf3[0].checked = true;
    if (document.childForm.cf3[1].checked)
       opener.document.parentForm.pf3[1].checked = true;       
    self.close();
    return false;
}
//--></SCRIPT>
</HEAD><BODY>
<FORM NAME="childForm" onSubmit="return updateParent();">
<BR><INPUT NAME="cf1" TYPE="TEXT" VALUE="">
</FORM></BODY></HTML>
Share Improve this question asked Apr 19, 2016 at 19:49 Sarah C. CorriherSarah C. Corriher 551 gold badge1 silver badge12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I ended up throwing most of the work in the question away, and not using a distinct window (but still using a pop-up). Here is the HTML:

<input type="text" id="text-source" value="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo consequat."/>
<button id="edit">Edit</button>

<div id="edit-popup" style="display: none">
     <textarea id="text-edit"/>
</div>

Here is the javascript that I settled upon:

$("#edit").click(function() {
        $("#text-edit").val($("#text-source").val());
    $("#edit-popup").dialog({
        height: 400,
        width: 600,
        modal: true,
        buttons: {
            "Close": function () {
        $("#text-source").val($("#text-edit").val());
                $(this).dialog("close");
        }
        }
    });
});

See it in action: https://jsfiddle/sazzy/5m621rLa/

I see no textarea in your child form, but assuming your childwindow has:

<textarea name="cf1"></textarea>

After opening the child window, you may need to wait for it to be ready/loaded, then do this:

var myVal = document.getElementsByName("pf1")[0].value;
childWindow.document.getElementsByName("cf1")[0].value = myVal;
发布评论

评论列表(0)

  1. 暂无评论