javascript 为什么用于隐藏输入的 document.getElementById 在 IE 中有效,但在 Chrome 中无效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6311078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why document.getElementById for hidden input works in IE but not Chrome?
提问by Melvin
i have a problem with this part of js code not working in Chrome but working in IE.
我有一个问题,这部分 js 代码不能在 Chrome 中工作,但可以在 IE 中工作。
this is my javascript code :
这是我的 javascript 代码:
function submitformWithPage(xpage)
{
document.getElementById('itempage').value = xpage;
alert(xpage);
document.searchForm.submit();
}
and this is my html code
这是我的 html 代码
<form name="searchForm" action="search.php" method="get">
<input type="text" name="search" value="<?php if(isset($_GET['search'])) { echo $_GET['search']; } ?>"/>
<input type="hidden" name="parameter" value="test" />
<input id="item" type="hidden" name="itempage" value="1" />
<input type="hidden" name="pageBigForward" value="10" />
<input type="hidden" name="pageSmallForward" value="1" />
<button style="" onclick="javascript: submitform()">Search</button>
</form>
I submitted the form by using this code and it works in IE but not in Chorme.
我使用此代码提交了表单,它在 IE 中有效,但在 Chorme 中无效。
<button style="" onclick="javascript: submitformWithPage(3);">3</button>
I am lost on how to solve this problem.
我不知道如何解决这个问题。
Can anyone help me ?
谁能帮我 ?
Thanks in advance.
提前致谢。
回答by Matt
Your input has the nameof itempage
, not the id.
您输入的名称为itempage
,而不是id。
<input id="item" type="hidden" name="itempage" value="1" id="itempage"/>
Using names-as-ids is only supported in IE5-7!
仅 IE5-7 支持使用名称作为 ID !
回答by Milan Aleksi?
Function getElementById is searching by attribute "id", not "name".
函数 getElementById 正在按属性“id”而不是“name”进行搜索。
So replace itempage with item and you are good to go.
所以用 item 替换 itempage,你就可以开始了。
回答by Krishna
Use id
instead of name
:
使用id
代替name
:
document.getElementById('item').value = xpage;
回答by Jrod
You have set the name but not the ID of your hidden element to "itempage"
您已将隐藏元素的名称而非 ID 设置为“itempage”
回答by Pointy
It works in IE because IE considers the "name" attribute as well as the "id" attribute for "getElementById()". It's a pretty stupid behavior, really, but it benefits your code because you don't have that string as an "id" value.
它在 IE 中有效,因为 IE 考虑了“名称”属性以及“getElementById()”的“id”属性。这是一个非常愚蠢的行为,真的,但它有益于您的代码,因为您没有将该字符串作为“id”值。