javascript 如何使用 <a4j:jsFunction><a4j:actionparam>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7580703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:36:43  来源:igfitidea点击:

How to use <a4j:jsFunction><a4j:actionparam>

javascriptjsfrichfacesajax4jsf

提问by Michael

I'm trying to use:

我正在尝试使用:

<script type="text/javascript">
      function myfunc() {
         var param = 4;
         alert("OK");
      }
</script>

I call the function like this:

我这样调用函数:

<a4j:jsFunction name="myfunc">
    <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/>
</a4j:jsFunction>

But it does not work. In what may be the reason?

但它不起作用。可能是什么原因?

回答by BalusC

You misunderstood the purpose of <a4j:jsFunction>. It autogeneratesa JavaScript function which you can then call from any JavaScript code in your view.

你误解了 的目的<a4j:jsFunction>。它会自动生成一个 JavaScript 函数,然后您可以从视图中的任何 JavaScript 代码调用该函数。

Your example,

你的例子,

<a4j:jsFunction name="myfunc">
    <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/>
</a4j:jsFunction>

will autogeneratethe following function

自动生成以下功能

<script>
    function myfunc(param) {
        // Here some specific JSF Ajax script which assigns "param"
        // to a managed bean property #{MyBean.myfield}
    }
</script>

You do notneed to define it yourself. You only need to invokeit yourself from some JavaScript code elsewhere. For example,

不是需要自己定义。您只需要从其他地方的一些 JavaScript 代码中自己调用它。例如,

<span onclick="myfunc(4)">click here to set 4 in MyBean.myfield</span>

or

或者

<script>
    function someOtherFunction() {
        var param = 4;
        myfunc(param);
    }
</script>

which is in turn to be used like

反过来又可以像

<span onclick="someOtherFunction()">click here to call someOtherFunction() which will in turn set 4 in MyBean.myfield</span>

See also:

也可以看看:

回答by Grim

<a4j:jsFunction 

is not used to call an function, it is used to define an function.

不是用来调用函数,而是用来定义函数。

So, if MyBean.myfield is an int-field you can set the value 2 using:

因此,如果 MyBean.myfield 是一个整数字段,您可以使用以下方法设置值 2:

<script>myfunc(2);</sript>

回答by Mark Kramer

There's a bunch of different ways to call that function.

有很多不同的方法可以调用该函数。

Two you will find particularly useful are:

您会发现特别有用的两个是:

This:

这:

<body onload="myfunc();">

Example: http://ultimatemmo.webege.com/Test.html

示例:http: //ultimatemmo.webege.com/Test.html

and this:

还有这个:

<a href="#" onclick="myfunc();">Click here to execute function</a>

Example: http://ultimatemmo.webege.com/Test2.html

示例:http: //ultimatemmo.webege.com/Test2.html

Edit:added examples.

编辑:添加示例。

回答by Reporter

According you snippet of code, you have never called your function. Add myfunc();within your script tag.

根据您的代码片段,您从未调用过您的函数。添加myfunc();脚本标签内。