![](/img/trans.png)
[英]prevent double form submit with javascript on h:commandLink in jsf under jdk 8
[英]JSF:: h:commandLink onsubmit(return validateRow(this)) problem
我在數據表的某一列中有ah:commandLink。
JSF
<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"
rendered="#{routeappcode.edit}"
value="save" onclick="return validateRow(this)"/>
生成的HTML是
<a id="routeappcodesummary:summarytable:2:save"
onclick="var cf = function(){return validateRow(this)};
var oamSF = function(){return oamSubmitForm('routeappcodesummary','routeappcodesummary:summarytable:2:save');};return (cf()==false)? false : oamSF();"
href="#">save</a>
Mojarra 1.2_15
<a href="#" onclick="var a=function(){return validateRow(this);};var b=function()
{if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('j_id_jsp_1765393453_2'),
{'j_id_jsp_1765393453_2:j_id_jsp_1765393453_3:0:j_id_jsp_1765393453_7':'j_id_jsp_1765393453
_2:j_id_jsp_1765393453_3:0:j_id_jsp_1765393453_7'},'');}return false};return (a()==false) ?
false : b();">test</a>
在這里,為onclick生成的javascript封裝了JSF標記中提供的腳本。
function validateRow(link){
//link is not a link object but some window object.
var parent = link.parentNode;
}
在javascript函數中,我們沒有鏈接對象,而是窗口對象。 原因是JSF標記中提供的腳本已封裝,並且由於該參考值的更改。
如何解決此問題,以便可以在腳本中獲取鏈接對象?
在IE 6中無法使用onmouseup 。
使用JSF 1.2
不知道為什么JSF會這樣做,但是這是使用jQuery獲取鏈接的解決方法:
<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"
rendered="#{routeappcode.edit}"value="save"
onclick="return validateRow($('a[id*=save]')[0])"/>
實際上,您無法為JavaScript函數提供this
的引用,因為onclick
代碼將封裝在JavaScript函數中。
您可以嘗試使用一些JavaScript代碼在validateRow()
函數中找到此link元素,如Stig Henriksen所建議的那樣 。
另一個想法是在鏈接上添加一個偽造的 CSS類,並使用該類搜索元素:
<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"
rendered="#{routeappcode.edit}" value="save"
onclick="return validateRow();" styleClass="saveLink"/>
然后,在您的JavaScript代碼中(我在這里使用jQuery,但您可以改用純JS):
function validateRow() {
// We retrieve a jQuery object:
var jQueryObject = $("a.saveLink");
// If you prefer to get a "pure" JavaScript object
var pureJavaScriptObject = $("a.saveLink").get(0);
// continue your work here...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.