最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why document.getElementById for hidden input works in IE but not Chrome? - Stack Overflow

programmeradmin1浏览0评论

i have a problem with this part of js code not working in Chrome but working in IE.

this is my javascript code :

function submitformWithPage(xpage)
{

  document.getElementById('itempage').value = xpage;
 alert(xpage);
  document.searchForm.submit();

}

and this is my html code

<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.

<button style="" onclick="javascript: submitformWithPage(3);">3</button>

I am lost on how to solve this problem.

Can anyone help me ?

Thanks in advance.

i have a problem with this part of js code not working in Chrome but working in IE.

this is my javascript code :

function submitformWithPage(xpage)
{

  document.getElementById('itempage').value = xpage;
 alert(xpage);
  document.searchForm.submit();

}

and this is my html code

<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.

<button style="" onclick="javascript: submitformWithPage(3);">3</button>

I am lost on how to solve this problem.

Can anyone help me ?

Thanks in advance.

Share Improve this question asked Jun 10, 2011 at 19:17 MelvinMelvin 3772 gold badges7 silver badges19 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 5

Your input has the name of itempage, not the id.

<input id="item" type="hidden" name="itempage" value="1" id="itempage"/>

Using names-as-ids is only supported in IE5-7!

Function getElementById is searching by attribute "id", not "name".

So replace itempage with item and you are good to go.

Use id instead of name:

document.getElementById('item').value = xpage;

You have set the name but not the ID of your hidden element to "itempage"

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.

发布评论

评论列表(0)

  1. 暂无评论