无法通过 JavaScript 单击隐藏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10317862/
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
Cannot click hidden button by JavaScript
提问by Viola
function openEditDialog(){
window.showModalDialog('/atmew/pages/asset/searchInclude/assetEdit.mew');
document.getElementById('reData').click();
alert('after rerender');
}
then there is a button:
然后有一个按钮:
<a4j:commandLink id="edit" action="#{searchController.openAssetEdit}"
oncomplete="openEditDialog()" immediate="true" ajaxSingle="true">
<h:graphicImage url="/images/edit_icon.png" </h:graphicImage>
<f:param name="arId" id="arId" value="#{vo.assetReceiving.id}"/>
</a4j:commandLink>
The Other button
其他按钮
<a4j:commandButton id="reData" reRender="data_grid" style="visibility: hidden;" onclick="javascript:alert('rerender clicked');"></a4j:commandButton>
the reData button does not get clicked. The console of IE does not show any message. How is this caused and how can I solve it?
reData 按钮没有被点击。IE 的控制台没有显示任何消息。这是怎么引起的,我该如何解决?
回答by BalusC
You need to use the JSF-generated HTML element ID, not the JSF component ID.
您需要使用 JSF 生成的 HTML 元素 ID,而不是 JSF 组件 ID。
Open the page in browser, rightclick and View Sourceand locate the JSF-generated HTML <input type="submit">
element of the <a4j:commandButton>
in the HTML source. It'll look like this:
在浏览器中打开页面,右键单击并查看源代码,然后在 HTML 源代码中找到 JSF 生成的 HTML<input type="submit">
元素<a4j:commandButton>
。它看起来像这样:
<input type="submit" id="someFormId:reData" ... />
You need to use exactly that ID in JavaScript, simply because JavaScript works on the HTML DOM tree, not on the JSF component tree.
您需要在 JavaScript 中准确使用该 ID,因为 JavaScript 工作在 HTML DOM 树上,而不是 JSF 组件树上。
document.getElementById('someFormId:reData').click();
Further you'd better be using <a4j:commandLink>
instead of <a4j:commandButton>
in order to get click()
to work.
此外,您最好使用<a4j:commandLink>
而不是<a4j:commandButton>
为了开始click()
工作。
回答by Tobias Krogh
Is it possible to call the onclick via document.getElementById("my-id").click()
? At least doing this in Chrome shows me the error "has no method 'click'". Or this possible when using jsf? (EDIT: sorry, might be a stupid question, but I never used jsf)
是否可以通过调用 onclick document.getElementById("my-id").click()
?至少在 Chrome 中这样做会显示错误“没有方法'点击'”。或者这在使用 jsf 时可能吗?(编辑:对不起,可能是一个愚蠢的问题,但我从未使用过 jsf)
I think the only reliable way to artificially create a click event on a native node is to do that via browser mechanisms:
我认为在本机节点上人为地创建点击事件的唯一可靠方法是通过浏览器机制来做到这一点:
function doEvent(element, eventType, event) {
// modern browsers
if (document.createEvent) {
event = document.createEvent("MouseEvents");
event.initMouseEvent(eventType, true, true, element.ownerDocument.defaultView,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
element.dispatchEvent(event);
// older browsers (IE6/7 for exmaple)
} else if (element.fireEvent) {
element.fireEvent("on"+eventType);
}
}
doEvent(document.getElementById("my-id"), "click");
regarding the ID consider the answer of BalusC
关于 ID 考虑BalusC的答案