JSF:: h:commandLink onsubmit(return validateRow(this)) probl

JSF:: h:commandLink onsubmit(return validateRow(this)) problem

本文关键字:this probl validateRow return onsubmit JSF commandLink      更新时间:2023-09-26

我在datatable中的一个列中有一个h: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>

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标签中提供的脚本被封装了,由于的值这个参考变化。

我怎样才能解决这个问题,这样我就可以在我的脚本中获得链接对象?

使用onmouseup无法在ie6中工作

使用JSF 1.2

你确实不能给你的JavaScript函数this的引用,因为onclick代码将被封装在JavaScript函数中。

你可以尝试使用一些JavaScript代码在你的validateRow()函数中找到这个链接元素,正如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...
}

我不知道为什么JSF会这样做,但是这里有一个使用jQuery获取链接的变通方法:

  <h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"  
    rendered="#{routeappcode.edit}"value="save" 
    onclick="return validateRow($('a[id*=save]')[0])"/>