2017-06-05 17 views
1

如何使用按鈕隱藏bpopup jquery?我有一個Ajax響應,如果數據返回錯誤失敗bpopup將被調用。然後在我的bpopup裏面我有一個「明白了!」按鈕,當用戶點擊它,它只會關閉bpopup如何隱藏bpopup jquery?

$.ajax({ 
    url: 'my_url', 
    type: "POST", 
    success: function(data){ 
     if(data.error == 'failed){ 
      $('#popup').bPopup({ 
       modalClose: false, 
       opacity: 0.6, 
       positionStyle: 'fixed' 
      }); 

      //then my bpopup has a button "Got it!" 
      $('.gotit').click(function(e) { 
       //Code the will hide the bpopup. 
      } 
     } 
    } 
}) 

我已經嘗試$('#popup).hide();但它並沒有完全關閉的bpopup

BTW這裏是我的彈出式HTML。

<div id="popup" style="display: none; min-width: 350px;"> 
    <span class="button b-close"><span>X</span></span> 
    <div class="col-lg-12"> 
      <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p> 
      <button class="btn btn-primary btn-sm pull-right gotit">Got it</button> 
    </div> 
</div> 
+0

你意識到上面的代碼中有一個錯誤,對嗎?成功函數中缺少''''。 – Scott

+0

對不起。 – p3ac3

回答

1

的所有

if(data.error == 'failed){這裏'首先被錯過了那麼添加它,使之: -

​​

關閉彈出窗口可以通過兩種方式

1.直接隱藏彈出窗口。

$('.gotit').click(function() { 

    $(this).closest('#popup').hide();//hidepop-up directly 

    // also you can use $(this).parent().parent('#popup').hide(); 
}); 

例子: -

$('.gotit').click(function() { 
 

 
    $(this).closest('#popup').hide();//hidepop-up directly 
 
    
 
    // also you can use $(this).parent().parent('#popup').hide(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="popup" style="display: block; min-width: 350px;"><!--changed disply:block to show you that how it will work --> 
 
    <span class="button b-close"><span>X</span></span> 
 
    <div class="col-lg-12"> 
 
      <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p> 
 
      <button class="btn btn-primary btn-sm pull-right gotit">Got it</button> 
 
    </div> 
 
</div>

2.Trigger密切的彈出按鈕單擊事件(如關閉按鈕的代碼已經寫入和工作)

$('.gotit').click(function() { 
    $('.b-close').click(); 
}); 

例子: -

$('.b-close').click(function() { //if your close button code is alwready written 
 
    $('#popup').hide(); 
 
}); 
 

 
$('.gotit').click(function(){ 
 
    $('.b-close').click(); // trigger close button clik event 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="popup" style="display: block; min-width: 350px;"><!--changed disply:block to show you that how it will work --> 
 
    <span class="button b-close"><span>X</span></span> 
 
    <div class="col-lg-12"> 
 
      <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p> 
 
      <button class="btn btn-primary btn-sm pull-right gotit">Got it</button> 
 
    </div> 
 
</div>

+0

第2個例子非常感謝! – p3ac3

+0

@ p3ac3很高興幫助你:) :) –