gpt4 book ai didi

javascript - 使用 onsubmit 或 onclick 打开窗口基于使用输入

转载 作者:行者123 更新时间:2023-12-03 04:12:19 25 4
gpt4 key购买 nike

我正在尝试找出这段代码:

<script>
function process()
{
var url="https://stackoverflow.com/questions/" + document.getElementById("url").value;
location.href=url;
return false;
}
</script>
<form onSubmit="return process();">
URL: <input type="text" name="url" id="url"> <input type="submit" value="go">
</form>

我想要发生的是,如果用户在页面Form value creates a url框中输入11811782将打开,但现在在提交时我想要打开模式/弹出窗口。我找到了这个页面: How to call multiple JavaScript functions in onclick event? 还有这个链接:onclick open window and specific size

我尝试将这些建议实现到代码中,但行不通。

最佳答案

由于我不能 100% 确定您是否想要一个弹出窗口或一个新窗口,这就是我的想法:

<form onSubmit="process(event)">
URL: <input type="text" name="url" id="url"> <input type="submit" value="go">
</form>
<script>
function process(e)
{
e.preventDefault();
var url="https://stackoverflow.com/questions/" + document.getElementById("url").value;
// location.href=url;
var popup = window.open(url,'mypopup','height=500,width=500');
if (window.focus) {popup.focus()}
return false;
}
</script>

preventDefault 已到位,以确保表单不会实际提交。

关于javascript - 使用 onsubmit 或 onclick 打开窗口基于使用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com