dun*_*can 5 javascript forms jquery internet-explorer
提交表单时,我使用Javascript更改了表单的URL。如果该URL包含哈希字符串(#),则Internet Explorer会忽略它,而只是在此之前提交到html部分。Firefox和Chrome都可以。
示范:
<script type="text/javascript">
function changeURL() {
var myform = document.getElementById('myform');
myform.setAttribute("action", "page2.html#hello");
return false;
}
</script>
<form id="myform" action="page1.html" method="get" onsubmit="changeURL()">
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
如果我将方法更改为“ post ”,那就很好。如果我使用“ get ”,则IE将登陆page2.html,但URL中不会出现#hello。
无论我使用jquery还是仅使用javascript,都会发生以下情况:
myform.action = "page2.html#hello";
myform.attr("action", "page2.html#hello");
myform.get(0).setAttribute("action", "page2.html#hello");
Run Code Online (Sandbox Code Playgroud)
有什么建议(假设我必须将该方法保留为“ get”,并且必须在URL中使用哈希,并且必须使用Javascript动态更改此操作)?
最后,我们决定只需更新即可window.location.href前往新位置,而不是提交表单。这似乎是一个奇怪的答案,但实际上我们处理表单的方式意味着这不是一个问题。即,我们禁用所有表单字段(因此通常不会将查询字符串附加到 URL),然后根据表单字段包含的内容生成几个不同的 SEO 友好样式 URL 之一,然后更新表单操作并提交表单。现在我们完成了所有这些,但不必提交表单,只需更改页面位置即可。
| 归档时间: |
|
| 查看次数: |
4660 次 |
| 最近记录: |