简体   繁体   English

jQuery对Primefaces组件的空验证

[英]jQuery empty validation for primefaces components

I am wonder how to check by jQuery if there are some fields empty. 我想知道如何通过jQuery检查是否有一些字段为空。 If yes I'll show a dialog. 如果是,我将显示一个对话框。

I was trying something like: 我正在尝试类似的东西:

$('input:text').each(function( index ) {    
    if( $(this).val().length == 0){ 
         ...
    } 
});

Unfortunately I saw that for example for <p:selectOneMenu> value is always "" , or what about p:selectOneRadio ? 不幸的是,我看到例如<p:selectOneMenu>值始终为"" ,或者p:selectOneRadio呢?

Each component in PrimeFaces has it's own way to check if the value is empty or not. PrimeFaces中的每个组件都有其自己的方式来检查值是否为空。

Taking that in mind I'll post how to check for empty values in inputText, selectOneMenu, selectOneRadio in "almost" generic way. 考虑到这一点,我将以“几乎”通用的方式发布如何在inputText,selectOneMenu,selectOneRadio中检查空值。

for (var propertyName in PrimeFaces.widgets) {
   if (PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.InputText) {
      if(PrimeFaces.widgets[propertyName].jq.val().length == 0) {
         PrimeFaces.widgets[propertyName].jq.css('background', 'red')
      } 
    } else if(PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.SelectOneRadio) {
      if(PrimeFaces.widgets[propertyName].checkedRadio.length == 0){
         PrimeFaces.widgets[propertyName].jq.css('background', 'red')
       }                                 
    } else if(PrimeFaces.widgets[propertyName] instanceof  PrimeFaces.widget.SelectOneMenu) {
      if(PrimeFaces.widgets[propertyName].getSelectedValue() == '') {
         PrimeFaces.widgets[propertyName].jq.parent().css('background', 'red')
      } 
    }
}

Basically we iterate over the widgets of PrimeFaces that we have in the page, and determine the way on how we check the value based on the component's type. 基本上,我们迭代页面中拥有的PrimeFaces小部件,并根据组件的类型确定如何检查值的方式。

Now to extend this, you need to write every individual component's way of validation. 现在要扩展它,您需要编写每个单独组件的验证方式。

A small working example can be found on github . 可以在github上找到一个小的工作示例。 And an online Demo . 和一个在线演示

Hope this helps. 希望这可以帮助。

Maybe something like that: 也许是这样的:

$('input:text').each(function( index ) {    
     if( $(this).val().length == 0 || $.trim($(this).val()) == ""){ 
         alert("empty");
     } 
});

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

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