如何在带有 Richfaces 的 JSF 中使用 javascript 函数提交表单,而没有表单名称或 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6102577/
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-25 19:27:23  来源:igfitidea点击:

How to submit a form using a javascript function in JSF w/ Richfaces, w/o the form name or id?

javascriptjsfrichfaces

提问by Adam

How would I go about submitting a form in Richfaces withoutthe form name or id from javascript? I was thinking I might be able to use the jsFunction tag, but I haven't been able to figure it out.

如果没有来自 javascript 的表单名称或 id,我将如何在 Richfaces 中提交表单?我想我也许可以使用 jsFunction 标签,但我一直无法弄清楚。

As a note, I can set the name of the function in jsFunction

注意,我可以在 jsFunction 中设置函数的名称

EDITRF 3.3, JSF 1.2

编辑RF 3.3、JSF 1.2

回答by BalusC

That's not possible without repeating the form ID and without knowing the exact location of the form in the HTML DOM tree as opposed to the element from where you'd like to submit the form.

如果不重复表单 ID 并且不知道表单在 HTML DOM 树中的确切位置,而不是您要提交表单的元素,这是不可能的。

You could at best use the EL function rich:element(). Imagine that you've a form like this:

您最多可以使用 EL 功能rich:element()。想象一下,你有一个这样的表单:

<h:form id="myform">

then you could use rich:element()as follows in JS context (in a JSF page!) to get the form as HTML DOM element:

那么你可以rich:element()在 JS 上下文中使用如下(在 JSF 页面中!)来获取表单作为 HTML DOM 元素:

<script>
    var form = #{rich:element('myform')};
    form.submit();
</script>

This get namely generated to the HTML as follows

这就是生成到 HTML 如下

<script>
    var form = document.getElementById('myformOrWhateverClientIdItHas');
    form.submit();
</script>

See also:

也可以看看:



Update: Or, when the JS function is invoked by an element inside the form itself, then just use this.formto get the parent form element of the HTML DOM element in question. E.g.

更新:或者,当 JS 函数被表单本身内部的元素调用时,则只this.form用于获取相关 HTML DOM 元素的父表单元素。例如

<h:selectBooleanCheckbox onclick="doSomething(this.form)" />

with

function doSomething(form) {
    // ...
    form.submit();
}

回答by Max Katz

You don't need the form name or id to submit using a4j:jsFunction.

您不需要使用 a4j:jsFunction 提交表单名称或 id。

回答by OliverLJ

If you want to use a4j:jsFunction, don't forget to add execute="@form" in order to submit all form

如果你想使用a4j:jsFunction,不要忘记添加execute="@form"以便提交所有表单