当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Access a form value in one frame from another frame

问题描述:

I have created two frames with two different forms as shown in link below:

http://demo.4page.info/framedemo/main.html (Please view in IE)

Now, when I click on Get Text button, a predefined value should display in the text box above it, and then when I click on the Copy Here button the same value from textbox1 should display in the text box above it.

So my question is how to access that value in second frames form by clicking copy here button?

Below is my code:

<!--main.html-->

<html>

<head>

<frameset cols="50%, 50%">

<frame name="f1" src="left.html" />

<frame name="f2" src="right.html" />

</frameset>

</head>

</html>

This successfully divides page into two frames.

<!--left.html-->

<html>

<head>

<link rel="stylesheet" type="text/css" href="style.css" />

<script type="text/vbscript">

dim msg

msg="Hello"

function func()

document.form1.text1.value=msg

end function

</script>

</head>

<body>

<form name="form1">

<table align="center" cellspacing="5">

<tr><td><input type="text" name="text1" /></td></tr>

<tr><td><input type="button" value="Get Text" onClick="func()" /></td></tr>

<tr><td><input type="reset" name="reset" value="Reset" /></td></tr>

</table>

</form>

</body>

</html>

left.html is working properly and accessing value on button click.

<!--right.html-->

<html>

<head>

<link rel="stylesheet" type="text/css" href="style.css" />

<script type="text/vbscript">

</script>

</head>

<body>

<form name="form2">

<table align="center" cellspacing="5">

<tr><td><input type="text" name="text2" /></td></tr>

<tr><td><input type="button" value="Copy Here!" onClick="??????" /></td></tr>

<tr><td><input type="reset" name="reset" value="Reset" /></td></tr>

</table>

</form>

</body>

</html>

What do I have to put in right.html?

网友答案:

You can access content of other frames via the frames property of the parent object, so something like this should work:

<script type="text/vbscript">
Sub CopyHere
  document.form2.text2.value = parent.frames("f1").document.form1.text1.value
End Sub
</script>
...
<form name="form2">
  <input type="text" name="text2" /></td></tr>
  <input type="button" value="Copy Here!" onClick="CopyHere()" />
</form>

Note that you may need to adjust your browser's security settings for the code to work.

On a more general note I'd recommend using id attributes instead of name attributes, because the latter is deprecated.

分享给朋友:
您可能感兴趣的文章:
随机阅读: