简体   繁体   English

Primefaces commandButton和Ajax

[英]Primefaces commandButton and ajax

I use a tabView component with many tabs. 我使用带有许多标签的tabView组件。 In many of them, I have form which are submitted by primefaces commandButton component. 在其中许多form中,我都有由primefaces commandButton组件提交的表单。

By default, PF commandButton using ajax mode but when I submit my form, my page seems to be fully loaded and my tabView component lost its index view (index 0 is rendered). 默认情况下,PF commandButton使用ajax模式,但是当我提交表单时,我的页面似乎已完全加载,我的tabView组件丢失了其索引视图(呈现索引0)。

Is that normal behaviour please ? 请问那是正常的行为吗?

I though that I would stay in the same index because it's ajax... 我虽然会保留在同一索引中,因为它是ajax ...

Looks like there is some naming container ( p:tabView maybe) that you better assign an id to it , so instead of getting prefix like j_idt16 (which could vary from time to time) you will get myTab0 , myTab1 etc prefix... 看起来似乎有一些命名容器(可能是p:tabView ),您最好为其分配一个id ,因此您将获得myTab0myTab1等前缀,而不是像j_idt16这样的前缀(可能会不时发生变化)...

for example <p:tabView id="myTab" 例如<p:tabView id="myTab"

Another thing you could do to be on the safe side is checking if the element exists before trying to select it with jquery and access its value, like this 为了安全起见,您可以做的另一件事是在尝试使用jquery选择元素并访问其值之前检查该元素是否存在,如下所示

if($('#j_idt16\\:register_location_choice_2_input').length > 0){
    //some code here
}

Ok, my problem is the JS validateRegisterForm function. 好的,我的问题是JS validateRegisterForm函数。 When I remove it, it works but I need it... 当我删除它时,它可以工作,但是我需要它...

I use it to check if validation form can be launched. 我用它来检查是否可以启动验证表单。

function validateRegisterForm(){    

if($('#j_idt16\\:register_location_choice_2_input').attr('checked')){

    if($('#j_idt16\\:register_galaxies_input').val() == 0){
        var galaxie = MUST_CHOOSE_GALAXY;
        alert(galaxie.charAt(0).toUpperCase() + galaxie.slice(1));
        return false;
    }

    if($('#j_idt16\\:register_solar_systems_input').val() == 0){
        var ss = MUST_CHOOSE_SOLAR_SYSTEM;
        alert(ss.charAt(0).toUpperCase() + ss.slice(1));
        return false;
    }

    if($('#j_idt16\\:register_positions_input').val() == 0){
        var position = MUST_CHOOSE_POSITION;
        alert(position.charAt(0).toUpperCase() + position.slice(1));
        return false;
    }

}


return true;    

} }

So how can I check fields values before sending and allowing or not validation form with ajax please ? 那么,如何在发送和允许使用ajax验证表单之前检查字段值?

EDIT : 编辑:

Ok, I solved my problem by launching validation inside my JS function with button type passed to button not submit and using remoteCommand component : 好的,我通过在JS函数中启动验证并将按钮type传递给button而不submit并使用remoteCommand组件来解决我的问题:

My JS function : 我的JS功能:

function validateRegisterForm(){    

if(...)
      validateForm();

} }

And my remoteCommand : 和我的remoteCommand

<p:remoteCommand name="validateForm" actionListener="#{login.registerAccount()}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM