【问题标题】:jsFunction's data is undefined when event starter is inside the form当事件启动器在表单内时,jsFunction 的数据未定义
【发布时间】:2013-12-05 13:49:57
【问题描述】:

这是我的情况:
我想测试是否无论如何链接java bean和xhtml,所以我使用richfaces,a4j:jsFunction来处理它。
我成功了——在某种程度上,事件启动器必须在表单之外触发,所以很奇怪!
我的代码有 3 个部分:xhtml、js、java
xhtml:

<button id="succeed" onclick="showMessage()"> succeed_test </button>
<!-- this is the succeeded button -->  

<h:form id="form1" prependId="true" >

        <button id="fail" onclick="showMessage()"> fail_test </button>
        <!-- this is the failed button -->  

        <a4j:jsFunction 
            name="showMessage"
            data="#{javaBeanTest.showThings()}"
            oncomplete="presentData(data)" 
            immediate="true" />

</h:form>

js:

function presentData(data){
    alert(data);
}

java:

@Name("javaBeanTest")
public class JavaBeanTest implements Serializable{
    public boolean showThings(){
        System.out.println("--JavaBeanTest.showThings()");
        return true;
    }
}

当点击「succeed_test」按钮时,我在控制台上得到一个「true」警告和「--JavaBeanTest.showThings()」,但在控制台上只有「undefined」警告和「--JavaBeanTest.showThings()」同时点击「fail_test」按钮。

显然差异是在形式内部或外部.....

困惑!!!!请!!!

ps.在我的 Richfaces 版本中,它适用于「data」而不是「event.data」,应该是 3.x
JDK 1.6
JBDS 4.1.0GA
接缝 2.2
JSF 1.2

【问题讨论】:

    标签: javascript jsf richfaces


    【解决方案1】:

    在您的情况下,如果 HTML 按钮在一个表单内,则它会提交表单(行为可能因浏览器而异)。您需要明确指定按钮类型或阻止默认提交行为,例如:

    <button id="fail" type="button" onclick="showMessage()">does not fail anymore</button>
    

    <button id="fail" onclick="showMessage(); return false;">does not fail anymore</button>
    

    W3C 建议始终指定 type 属性:

    提示:始终指定元素的类型属性。 不同的浏览器可能使用不同的默认类型 元素。

    参考:http://www.w3schools.com/tags/att_button_type.asp

    【讨论】:

    • 它工作!非常感谢!
    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多