简体   繁体   English

jQuery Click Function仅工作一次

[英]JQuery Click Function working for just one time

I'm trying to use jQuery click method to show and hide a part of my sections after clicking on a certain part of the page . 我试图使用jQuery click方法在单击页面的特定部分后显示和隐藏部分内容。

My HTML code: 我的HTML代码:

<section class="apply_section text-center center-block" id="apply_section">
      <div class="apply_container">

        <div class="container">
          <div class="row apply_row">

            <div class="col-md-8 apply_form">
              <form class="form" action="mail.php" method="POST">
               <div class="col-md-6">
                  <div class="form-group">
                    <input class="form-control" type="text" placeholder="" required="required" name="firstName">
                  </div>

                <div class="col-md-6">
                  <form class="form">
                    <div class="form-group">
                      <textarea class="form-control" placeholder="" required="required" name="message"></textarea>
                    </div>
                  </form>
                  <input class="btn btn-success btn-block" type="submit" value=""></input>
                </div>

              </form>
            </div>

            <div class="col-md-4 apply_image">
              <a><img src="images/icon.png" alt=""></a>
            </div>

          </div>
        </div>

      </div>

    </section>

The jQuery script : jQuery脚本:

$('#apply_section .apply_image a').click(function(){
  if($('#apply_section .apply_form').css('display','none')) {
    $('#apply_section .apply_form').css('display','inline-block');
  }else {$('#apply_section .apply_form').css('display','none');}
});

The problem is that the clicking method take the order just for one time, If I click on the image the form will appear, but after that if I need to hide it again and clicking the image doesn't work! 问题是单击方法仅接受订单一次,如果单击图像,将显示该表单,但是此后,如果我需要再次隐藏它,则单击图像不起作用!

That's not how you check css property. 那不是您检查CSS属性的方式。

$('#apply_section .apply_image a').click(function() {
  if ($('#apply_section .apply_form').css('display') == 'none') { // Here, check if display value is 'none'
    $('#apply_section .apply_form').css('display', 'inline-block');
  } else {
    $('#apply_section .apply_form').css('display', 'none');
  }
});

Another way using toggle: 使用切换的另一种方式:

 $('#apply_section .apply_image a').click(function() {
     var boolean = $('#apply_section .apply_form').css('display') == 'none';
      $('#apply_section .apply_form').toggle(boolean)
 });   

According to Jquery toggle docs: The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline. 根据Jquery toggle docs: The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline. The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

A better way to do something like that is using a toggle to a class: 做类似事情的更好方法是使用切换到类的方法:

CSS 的CSS

.hide {
    display:none; 
}

jQuery jQuery的

$('#apply_section .apply_image a').click(function(){
        $('#apply_section .apply_form').toggleClass('hide');
});

Simplify your code using toggle(): 使用toggle()简化代码:

$('#apply_section .apply_image a').click(function(){
    $('#apply_section .apply_form').toggle();
});

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

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