如果您有一个JSF <h:commandLink> (它使用<a>的onclick事件来提交当前表单),那么如何在执行操作之前执行JavaScript (比如要求删除确认)?
发布于 2008-09-16 15:33:54
<h:commandLink id="myCommandLink" action="#{myPageCode.doDelete}">
<h:outputText value="#{msgs.deleteText}" />
</h:commandLink>
<script type="text/javascript">
if (document.getElementById) {
var commandLink = document.getElementById('<c:out value="${myPageCode.myCommandLinkClientId}" />');
if (commandLink && commandLink.onclick) {
var commandLinkOnclick = commandLink.onclick;
commandLink.onclick = function() {
var result = confirm('Do you really want to <c:out value="${msgs.deleteText}" />?');
if (result) {
return commandLinkOnclick();
}
return false;
}
}
}
</script>可以通过将对confirm()的调用替换为对另一个函数的调用来执行其他Javascript操作(如验证表单输入等)。
发布于 2010-12-28 06:13:13
可以像这样简化
onclick="return confirm('Are you sure?');"发布于 2011-05-25 00:07:33
这对我很有效:
<h:commandButton title="#{bundle.NewPatient}" action="#{identifController.prepareCreate}"
id="newibutton"
onclick="if(confirm('#{bundle.NewPatient}?'))return true; else return false;"
value="#{bundle.NewPatient}"/>https://stackoverflow.com/questions/73628
复制相似问题