让 ENTER 键提交 iframe 的 url 表单
因此,我有一个文本框用作 iframe 的地址栏。好吧,之前,我需要一个带有以下脚本的“GO”按钮:
<script language="javascript">
function LoadPage(){
var objFrame=document.getElementById("myIframe");
objFrame.src=document.getElementById("URL").value;
}
</script>
“GO”按钮代码:
<input type="image" src="http://polaris.umuc.edu/~mahearn/images/arrowbutton.png"
height="20" width="20" class="frmSubmit" value="Go" onclick="LoadPage()" />
我希望当用户在文本框中键入内容时按“ENTER”时激活该代码。
这就是我尝试过的。发生的情况是,当我单击文本框时,事件被激活,而不是按下按键来激活事件。
input type="text" value="http://" class="frmUrlVal" id="URL1" onkeypress="return (LoadPage(1))"/>
So, I have a text box which is used as the address bar for the iframe. Well, before, I needed a "GO" button with this script:
<script language="javascript">
function LoadPage(){
var objFrame=document.getElementById("myIframe");
objFrame.src=document.getElementById("URL").value;
}
</script>
"GO" button code:
<input type="image" src="http://polaris.umuc.edu/~mahearn/images/arrowbutton.png"
height="20" width="20" class="frmSubmit" value="Go" onclick="LoadPage()" />
I want that code to be activated when the user presses "ENTER" while typing in the text box.
This is what I tried. What happened was that instead of a key pressed down that activated the event, when I clicked on the text box, the event was activated.
input type="text" value="http://" class="frmUrlVal" id="URL1" onkeypress="return (LoadPage(1))"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
OnSubmit
事件处理程序在文本框周围放置一个表单。确保在处理程序末尾返回false
以防止它实际提交表单。You could put a form around the textbox with an
OnSubmit
event handler. Be sure you returnfalse
at the end of the handler to prevent it from actually submitting the form.下面的脚本应该适合你。每次按下按键时,都会在 if 语句中评估该按键。如果按下 Enter 键,则调用 LoadPage,是否是任何其他键?我将 Loadpage 修改为仅作为示例购买的警报,您可以让它执行您想要的任何操作。工作jsfiddle: http://jsfiddle.net/zBD8V/
The below script should work for you. On each key press the key is evaluated in the if statement. If enter was pressed LoadPage is called is it any other key it is not. I modified Loadpage to just be an alert for the example buy you can have it do what ever you want. Working jsfiddle: http://jsfiddle.net/zBD8V/