开发者

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

I have a h:commandLink in one of the column in datatable.

JSF

<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"  
rendered="#{routeappcode.edit}"
value="save" onclick="return validateRow(this)"/>

Generated HTML is

 <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>

Here the generated javascript for onclick encapsulates the script provided in JSF tag.

function validateRow(link){
    //link is not a link object but some window object.
    var parent = link.parentNode;
}

Here in javascript function we don't get a link object but a window object. Reason is script provided in JSF tag was encapsulated 开发者_高级运维and due to that value of this reference changes.

How can I solve this problem, so that I can get link object in my script?

Using onmouseup won't work in IE 6.

Using JSF 1.2


You can't indeed give your JavaScript function the reference to this, as the onclick code will be encapsulated in a JavaScript function.

You can try to find this link element in your validateRow() function using some JavaScript code, as the one proposed by Stig Henriksen.

Another idea is to add a fake CSS class on your link, and search you element using this class:

<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"
    rendered="#{routeappcode.edit}" value="save"
    onclick="return validateRow();" styleClass="saveLink"/>

then, in your JavaScript code (I use jQuery here, but you can use pure JS instead):

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...
}


Don't know why JSF does that, but here is a workaround for getting the link using jQuery:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜