h:commandLink不触发form.submit事件 [英] h:commandLink not firing the form.submit event

查看:116
本文介绍了h:commandLink不触发form.submit事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面加载后,我将执行以下JS脚本,当提交任何表单时,我会使用该脚本显示弹出窗口(请稍候...).

When my page is loaded I execute the following JS script, which I use to display a popup saying (please wait...) when any form is submitted.

jQuery(function(){
  jQuery("form").submit(function(){jQuery('#wait-link').trigger('click');return true;});
});

当使用 h:commandButton 标记时,此方法工作正常,但是,当我使用 h:commandLink 标记时,则无效,因为该表单是由Java脚本提交的(如下所示,从jar jsf-impl.jar文件jsf.js中提取)

This works fine when using h:commandButton tag, however when I use the h:commandLink tag it doesn't work because the form is submitted by java script (from the file jsf.js in the jar jsf-impl.jar) as shown below

mojarra.jsfcljs = function jsfcljs(f, pvp, t) {
  mojarra.apf(f, pvp);
  var ft = f.target;
  if (t) {
    f.target = t;
  }
  f.submit();
  f.target = ft;
  mojarra.dpf(f);
};

为解决此问题,我将 jsf.js 文件复制到 WEB-INF/resources/javax.faces/jsf.js 下,并对其进行了修改以触发表单提交jQuery的方法.效果很好,但是:

To solve this problem I copied the jsf.js file under WEB-INF/resources/javax.faces/jsf.js and modified it to trigger the form submit method using jQuery. This works fine but:

1)我不喜欢触摸 jsf.js 文件这一事实,因为在新版本的JSF中它可能会更改.

1) I don't like the fact that I am touching the jsf.js file since it may change in newer releases of JSF.

2)我不喜欢在 jsf.js 文件中使用jQuery的事实.

2) I don't like the fact that I am using jQuery inside the jsf.js file.

是否有更好的解决方案来解决此问题?

Is there a better solution to solve this problem?

推荐答案

正如Bodgan所建议的,一种更好的解决方案是在加载页面时执行以下代码,我不知道JS中的术语是什么,但似乎对我来说很重要.

As Bodgan suggested a better solution is to execute the following piece of code when the page is loaded, I don't know what the term is in JS but seems an override for me.

jQuery(function(){
  var oldjsfcljs = mojarra.jsfcljs; // save a pointer to the old function

  mojarra.jsfcljs = function jsfcljs(f, pvp, t) {
    jQuery(jq(f.id)).trigger('submit');
    oldjsfcljs.apply(this, arguments);
  };
});

function jq(myid) {
  return '#' + myid.replace(/(:|\.)/g, '\\$1');
}

注意:jq()方法仅用于转义表单ID中的特殊字符.

Note: the jq() method is only used to escape the special characters in the form ID.

更新:上面的代码似乎可以正常工作,但是如果您使用的形式的参数不起作用,那么我必须按如下所示重写mojarra.jsfcljs函数:

update: the above code seemed to work but if you have parameters in the form it wont so I had to rewrite the mojarra.jsfcljs function as follows:

jQuery(function(){
  mojarra.jsfcljs = function jsfcljs(f, pvp, t) {
    mojarra.apf(f, pvp);
    var ft = f.target;
    if (t) {
      f.target = t;
    }

    // trigger the submit event manually.
    jQuery(jq(f.id)).trigger('submit');

    f.submit();
    f.target = ft;
    mojarra.dpf(f);
  };
};

这篇关于h:commandLink不触发form.submit事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆