【发布时间】:2014-07-01 16:06:39
【问题描述】:
好的。我有一个表单,当提交时,它会通过 JavaScript 来决定将用户带到下一个页面。
当我在我的 IDE (Coda 2) 中预览它以及在 Chrome 中预览时,一切正常。但是,当我在 Firefox、IE(任何版本)或 Safari 中打开同一页面时,我无法通过第一页。
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
<link href="eh_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav id="navWrapper" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand">Site Title</a>
</div>
<div class="link-container">
<ul>
<li><a href="http://www.link.net">LinkText</a></li>
<li><a href="mailto:support@link.net?subject:Help”>Help</a></li>
</ul>
</div>
</div>
</nav>
<div class="main-container">
<div class="page-container">
<form onsubmit="OpenWindow()" id="page1" action="" method="post">
<fieldset id="mainSelection">
<label><input type="radio" class="radio-button" value="A" name="sel1"> Option A</label><br />
<label><input type="radio" class="radio-button" value="B" name="sel1"> Option B</label><br />
<label><input type="radio" class="radio-button" value="C" name="sel1"> Option C</label><br />
<label><input type="radio" class="radio-button" value="D" name="sel1"> Option D</label><br />
</fieldset><br />
<input type="submit" value="Next" id="submitButton" form="page1">
</form>
</div><!--page-container-->
</div><!--main-container-->
<footer class="site-footer">
Footer Text
</footer>
<script type="text/javascript" language="javascript">
function OpenWindow() {
var choice = document.getElementById("page1");
choice = choice.sel1.value;
if (choice == 'A') {
window.open('result1.html','_self');
} else if (choice == 'B') {
window.open('page2.html','_self');
} else if (choice == 'C') {
window.open('page3.html','_self');
} else if (choice == 'D') {
window.open('page4.html','_self');
}
return: false;
}
</script>
</body>
</html>
我不知道这里发生了什么。非常感激任何的帮助。
根据几个建议,我在脚本末尾添加了 return: false; 语句,但仍然无法正常工作。
【问题讨论】:
-
按 F12。转到脚本调试器。调试您的页面。 (另外,你的 openWindow 函数应该返回 false)
-
我对您在页面中包含 jquery 但在代码中使用纯 JS 这一事实很感兴趣 但我的猜测是正如 EricLaw 提到的那样,因为您的 openwindow 函数没有返回 false 表单正在将数据提交到同一页面。
-
@user1094553 这是我第二次弄乱 JS - 我不确定是否需要包含 jQuery 才能使其正常运行。
-
@EricLaw 我打开了 Firefox 调试窗口。在将
return: false;添加到我的脚本底部之前,当我提交表单时,它会打开该功能,但只是挂在加载消息上。添加return: false;后,它会使用javascript:OpenWindow()填充URL 字段并在显示屏上打印false...有什么想法吗? -
@user1094553 查看最后一条评论。
标签: javascript html internet-explorer google-chrome firefox