仅在div未显示时才预先设置?

时间:2015-10-21 11:04:51

标签: javascript jquery html css

基本上,我只想在我前面提到的div尚未显示时才添加div。

$(".loginForm").submit(function() {
    var username = $("input.username").val();
        var password = $("input.password").val();
        var errorvisible = $("body").has(".alert.error");
    if (!username || !password && errorvisible == false) {
        $('body').prepend("<div class=\"error alert\">One or more field(s), have been left empty</div>");

        setTimeout(function() {
            $('.error.alert').fadeOut('1000', function() {
                $('.error.alert').remove();
            });
        }, 6000);

        event.preventDefault();
    }

});

目前,我正在尝试这样做,以便jquery只会执行if if if语句,如果错误当前不可见但是它不起作用...

        <div class="pageCont">

        <div class="title">
            <h1>LetsChat</h1>
            <h4>The new way of interaction.</h4>
        </div>

        <div class="login box">
        <form method="POST" action="#" class="loginForm">
            <input type="text" class="loginInput username" placeholder="Aquinas Email or Number" /><br>
            <input type="password" class="loginInput password" placeholder="Password" /><br>
            <div class="checkBox">
                <input type="checkbox" id="checkBoxLink">
                <label for="checkBoxLink">Remember Me</label>
                <a href="forgotpass" class="forgotpass cfix">Forgotten your password?</a>
            </div>
            <input type="submit" value="Submit" class="login btn green" />
            <input type="reset" value="Clear Fields" class="reset btn green" />
        </form>
        </div>
        <div class="signup box">
            <form method="POST" action="#" class="signupForm">
                <input type="text" class="signupInput" placeholder="First Name" /><br>
                <input type="text" class="signupInput" placeholder="Last Name" /><br>
                <input type="text" class="signupInput" placeholder="Aquinas Email" /><br>
                <input type="password" class="signupInput" placeholder="Password" /><br>
                <input type="submit" class="purple btn signup" value="Sign Up Today!">
            </form>
        </div>
    </div>

3 个答案:

答案 0 :(得分:2)

如果我正确理解你的问题,下面应该有用吗?

  $(".loginForm").submit(function() {
    var username = $("input.username").val();
    var password = $("input.password").val();
    var errorvisible = $("body").has(".alert.error").length;

    if (!username || !password) 
    {
        if(errorvisible == 0)
            $('body').prepend("<div class=\"error alert\">One or more field(s), have been left empty</div>");

        setTimeout(function() {
            $('.error.alert').fadeOut('1000', function() {
                $('.error.alert').remove();
            });
        }, 6000);

        event.preventDefault();         
    }
});

答案 1 :(得分:0)

我多次使用过这种代码:

if(!$('#myFancyDiv').is(':visible')) {
    //prepend or do whatever you want here
}

顺便说一句,为了清楚起见,此代码检查myFancyDiv&#39;是可见。

答案 2 :(得分:-1)

您可以检查$query = ' { "slug" : "fashion" }'; $primaryCategory = Category::findFirst(array(json_decode($query,true))); $cat = new Category(); // new secondary category $cat->name = 'Secondary'; $cat->slug = 'secondary'; $cat->taxonomy = true; $cat->ancestors = array(); $primary = array(); $primary['_id'] = $primaryCategory->_id; $primary['name'] = $primaryCategory->name; array_push ( $cat->ancestors, $primary ); $cat->save(); 元素是否存在,如果不存在,则不要创建新元素

&#13;
&#13;
.error.alert
&#13;
$(".loginForm").submit(function() {
  var username = $("input.username").val();
  var password = $("input.password").val();
  var errorvisible = $("body").has(".alert.error");
  if (!username || !password && errorvisible == false) {

    if (!$('.error.alert').length) {
      $('body').prepend("<div class=\"error alert\">One or more field(s), have been left empty</div>");
      setTimeout(function() {
        $('.error.alert').fadeOut('1000', function() {
          $(this).remove();
        });
      }, 6000);
    }

    event.preventDefault();
  }

});
&#13;
&#13;
&#13;