Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当method=get且URL包含哈希值时,使用Javascript更改表单操作时IE中存在错误_Javascript_Jquery_Forms_Internet Explorer - Fatal编程技术网

当method=get且URL包含哈希值时,使用Javascript更改表单操作时IE中存在错误

当method=get且URL包含哈希值时,使用Javascript更改表单操作时IE中存在错误,javascript,jquery,forms,internet-explorer,Javascript,Jquery,Forms,Internet Explorer,我正在使用Javascript在您提交表单时更改表单的URL。如果该URL包含哈希字符串(#),则Internet Explorer将忽略该字符串,并提交到之前的html部分。Firefox和Chrome都很好 演示: <script type="text/javascript"> function changeURL() { var myform = document.getElementById('myform'); myform.setAttribute("a

我正在使用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>

任何建议(假设我必须将该方法保持为“get”,并且我必须在URL中使用哈希,并且我必须使用Javascript动态更改此操作)?

我相信IE只是与哈希行为不同,我不认为它是用于此庄园的

下面的任何javascript都不会产生相同的结果…显示在FF中而不是IE中

<form action="#test" method="get">
    <input type="text" value="test" />
    <input type="submit" />
</form>


至少你知道这不是javascript问题。关于问号lol-oops,我撒了谎。

我自己在IE8中进行的测试表明,它确实坚持哈希(
#hello
)位于URL中的查询字符串(
?foo=bar
)之后。遗憾的是,您的表单没有为您这样做,并且在提交表单时无法强制它这样做

请尝试在表单中对哈希进行编码:

<script type="text/javascript">
function changeURL() {
    var hidden = document.createElement('input');
    hidden.setAttribute("type", "hidden"); 
    hidden.setAttribute("name", "hash"); 
    hidden.setAttribute("value", "hello"); 
    var myform = document.getElementById('myform');
    myform.setAttribute("action", "page2.html"); 
    myform.appendChild(hidden); 
    // return false;
}
</script>
<form id="myform" action="page1.html" method="get" onsubmit="changeURL()">
    <input type="submit">
</form>

函数changeURL(){
var hidden=document.createElement('input');
setAttribute(“类型”、“隐藏”);
setAttribute(“名称”、“哈希”);
setAttribute(“value”、“hello”);
var myform=document.getElementById('myform');
setAttribute(“action”,“page2.html”);
myform.appendChild(隐藏);
//返回false;
}
在page2.html的顶部,将其提取出来:

<script type="text/javascript">
var qs = window.location.search.substring(1);
var qsarr = qs.split("&");
for (var i=0; i<qsarr.length; i++) {
    var tarr = qsarr[i].split("=");
    if (tarr[0]==="hash") {
        window.location.hash = tarr[1];
    }
}
</script>

var qs=window.location.search.substring(1);
var qsarr=qs.拆分(“&”);

对于(var i=0;i最后,我们决定只更新
window.location.href
以转到新位置,而不是提交表单。这似乎是一个奇怪的答案,但实际上我们处理表单的方式意味着这不是一个问题。即,我们禁用了所有表单字段(因此通常不会将查询字符串附加到URL),然后根据表单字段所包含的内容生成一个不同的SEO友好型URL,然后更新表单操作并提交表单。因此,现在我们做了所有这些,但不必麻烦提交表单,只需更改页面位置。

URL中的哈希之前不应该有
get
查询字符串吗?如果是,第一个我能想到的解决方案是将散列值放在一个隐藏的表单字段中,这样它就可以在查询字符串中传递,然后在另一端使用JavaScript将其转换回散列。@duncan如果您使用GET提交,则会创建一个querystring。
<script type="text/javascript">
var qs = window.location.search.substring(1);
var qsarr = qs.split("&");
for (var i=0; i<qsarr.length; i++) {
    var tarr = qsarr[i].split("=");
    if (tarr[0]==="hash") {
        window.location.hash = tarr[1];
    }
}
</script>