我正在用bootstrap做一个网站。

基本上,我想在主页中使用一个模态,通过Hero Unit中的按钮来调用。

按钮代码:

<button type="button" 
    class="btn btn-warning btn-large" 
    data-toggle="modal"
    data-target="#myModal">Open Modal</button>

模态代码:

<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">In Costruzione</h3>
  </div>
  <div class="modal-body">
    <p>Test Modal: Bootstrap</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Chiudi</button>
    <button class="btn btn-warning">Salva</button>
  </div>
</div>

问题是,只要我点击按钮,模态就会淡入,然后立即消失。

我很感激你的帮助。

此外,如何改变下拉三角形的颜色(指向向上,没有悬停)?我正在做一个基于橙色和棕色的网站,那个蓝色的东西真的很烦人。


当前回答

如果使用jquery两次附加事件监听器,也会出现这个问题。 例如,你可以在不解除绑定的情况下设置:

            $("body").on("click","#fixerror",function(event){
                $("#fixerrormodal").modal('toggle')
            })

如果发生这种情况,事件将连续触发两次,模态将消失。

            $("body").on("click","#fixerror",function(event){
                $("#fixerrormodal").modal()
                $("#fixerrormodal").modal('toggle')
            })

参见示例:http://embed.plnkr.co/i5xB1WbX7zgXrbDMhNAW/preview

其他回答

今天我自己也遇到了这个问题,而且碰巧只是在Chrome上。是什么让我意识到这可能是一个渲染问题。 移动特定的模式到它自己的渲染层解决了这个问题。

#myModal {
  -webkit-transform: translate3d(0, 0, 0);
}

我有同样的问题,因为我切换了我的模式两次,如下所示:

statusCode: {
    410: function (request, status, error) { //custom error code

        document.getElementById('modalbody').innerHTML = error;
        $('#myErrorModal').modal('toggle')
    }
},
error: function (request, error) {

    document.getElementById('modalbody').innerHTML = error;
    $('#myErrorModal').modal('toggle')
}

我删除了一个出现项:

(“# myErrorModal美元)。“充气”(资本)

它像魔法一样起作用!

我遇到了@gpasse在他的例子中显示的相同症状。这是我的密码……

<form...>
  <!-- various inputs with form submit --->

  <!-- button opens modal to show dynamic info -->
  <button id="myButton" class="btn btn-primary">Show Modal</button>
</form>

<div id="myModal" class="modal fade" ...>
</div>

<script>
  $( '#myButton' ).click( function() {
    $( '#myModal' ).modal();
  )};
</script>

正如@Bhargav所建议的,我的问题是由于按钮试图提交表单,并重新加载页面。我将按钮更改为<a>链接如下:

<a href="#" id="myButton" class="btn btn-primary">Show Modal</a>

...它解决了这个问题。

注意:我还没有足够的声誉来简单地添加评论或点赞,我觉得我可以补充一点澄清。

又是一个原因/解决方案!我在JS代码中有:

$("#show_survey").click(function() {
  $("#survey_modal").modal("show")
})

但是我的HTML代码已经写成了:

<a class="small-text link" data-target="#survey_modal" data-toggle="modal" href="#" id="show_survey">Take a quick 7 question survey!</a>

这是复制输入代码并导致模态打开然后关闭的另一种排列方式。在我的例子中,数据目标和数据切换在html按照引导文档已经触发模式工作。因此,JS代码是冗余的,当删除它时,它可以工作。

我在移动设备上测试时也遇到过同样的问题,这个技巧对我来说很管用

<a type="submit" class="btn btn-primary" data-toggle="modal" href="#myModal">Submit</a> 

将按钮更改为锚定标签应该可以工作,问题是由于它的类型按钮,因为它试图提交,所以模式立即消失。并且也从模态中移除隐藏隐藏淡出给予 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria- labledby ="myModalLabel" aria-hidden="true"> 希望这对你有用。