我想弄清楚这段代码:
<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>
我想要发生的是,如果用户在框中输入11811782,页面Form value creates a url将打开,但现在在onsubmit上我希望打开模态/弹出窗口。我找到了这个页面: How to call multiple JavaScript functions in onclick event? 还有这个链接:onclick open window and specific size
我尝试将这些建议实现到代码中,但它不起作用。
答案 0 :(得分:1)
因为我不是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已经到位,以确保表单实际上没有提交。