代码之家  ›  专栏  ›  技术社区  ›  Amra

jquery检查控件是否为空并显示!“无”,为什么不起作用?

  •  0
  • Amra  · 技术社区  · 16 年前

    下面的if语句。

    emptyFields = false;
       $(".ClassNanme").each(function() {
          if(($.trim($(this).val()) == "") && ($(this).css('display') != 'none' ) {
            emptyFields = true;
        return false; // break out of the each-loop
          }
       });
    

    但是不起作用,我不知道如何使用jquery检查css属性display是否设置为none。

    此if语句应在其中一个对象为空或其css属性显示其not set为none时选择。

    检查值是否为空有效,我要做的是检查对象是否隐藏(或显示:无)。

    谢谢。

    Cesar。

    2 回复  |  直到 16 年前
        1
  •  5
  •   brettkelly    16 年前
       emptyFields = false;
       $(".ClassName").each(function() {
          if($.trim($(this).val()) === "" && $(this).is(":hidden")) {
            emptyFields = true;
            return false; // break out of the each-loop
          }
       });
    

    使用 is(":hidden") 在我看来,比检查实际的css值更干净,更容易阅读。试试看。

    另外,在与空字符串进行比较时使用===(类型相等)。

    编辑 :gah,将“visible”更改为“hidden”-我的错误。

        2
  •  1
  •   SLaks    16 年前

    你在找jquery的 visiblity selectors .

    例如:

    if ($(this).is(':hidden'))