javascript Onclick 事件在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19526615/
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
Onclick event not working in IE
提问by stash_man
I am making a PHP webpage for database search using JavaScript (search suggest). I used divto show the search suggest items.When the user clicks on this a details page for that item opens. It seems to work fine in Firefox bt it doesnot work on IE.
我正在使用 JavaScript 制作一个用于数据库搜索的 PHP 网页(搜索建议)。我过去常常div显示搜索建议项目。当用户单击此选项时,该项目的详细信息页面会打开。它似乎在 Firefox 中运行良好,但在 IE 上不起作用。
HTML part:
HTML部分:
<form method="post" name="searchform" id="searchform">
<table class="nostyle" width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%">Customer
<input type="text" name="search_customer" id="customer_name" class="input-text" onKeyUp="searchsuggest();" autocomplete="off"/>
<br /><div id="search_suggest" onclick="document.getElementById('searchform').submit();"> </div></td>
</tr>
</table>
</form>
Please help me, I'm a newbie!
请帮帮我,我是新手!
回答by Diodeus - James MacFarlane
You don't need an ONCLICKevent. type=submitalready does this for you, but the element needs to be an input, not a div.
您不需要ONCLICK事件。type=submit已经为您执行了此操作,但元素必须是input,而不是div。
回答by Diver
Take a look at http://jsfiddle.net/LM4Sg/1/you can click on the click here in ie and it works. If you look at http://jsfiddle.net/LM4Sg/you can click around the screen and never hit the EMPTY div.
看看http://jsfiddle.net/LM4Sg/1/你可以点击 ie 中的 click here 并且它可以工作。如果您查看http://jsfiddle.net/LM4Sg/,您可以在屏幕上四处点击而不会点击 EMPTY div。
You need to put something in it to click.
你需要在里面放一些东西才能点击。
<div id="search_suggest" type="submit" onclick="document.getElementById('searchform').submit();">Click Here</div>
If you just want to leave the space between the div tags use an & nbsp; instead, this also makes it so you can click in ie. See the fiddle - http://jsfiddle.net/LM4Sg/4/
如果您只想在 div 标签之间留出空间,请使用 & nbsp; 相反,这也使您可以单击 ie。见小提琴 - http://jsfiddle.net/LM4Sg/4/
<div id="search_suggest" onclick="alert('searchform');"> </div>
(changed to use an alert to simplify things.)
(改为使用警报来简化事情。)
回答by stash_man
It works now... I changed my javascript
它现在有效......我改变了我的javascript
var search_suggest = document.getElementById(customer_name);
search_suggest.onclick = function(){ document.getElementById('searchform').submit();
Tried doing it with jQuery bt still did not work and don't know why IE did not work with my previous code.
尝试用 jQuery bt 做它仍然不起作用,不知道为什么 IE 不能使用我以前的代码。

