如果选择了选项,则显示引导模式

时间:2016-06-05 14:42:50

标签: javascript jquery css twitter-bootstrap

我有一个包含下拉列表和一些选项的表单:

<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>

我还有一个Bootstrap模态窗口:

    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>Some text in the modal.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>

如果选择值“捐赠”,我想显示Bootstrap模式。

我试过这样做:

$("#type").bind("change", function () {
    $('#myModal').modal('show')(this.value === 'donations'); 
}).change();

模态默认打开,所以我知道我的目标是模态,但我只想在选择“捐赠”选项时显示。

有什么想法吗?

1 个答案:

答案 0 :(得分:10)

你试试这种方式..

$("#type").on("change", function () {        
    $modal = $('#myModal');
    if($(this).val() === 'donations'){
        $modal.modal('show');
    }
});

  $("#type").on("change", function () {        
      $modal = $('#myModal');
      if($(this).val() === 'donations'){
        $modal.modal('show');
    }
 });
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>
<!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>Some text in the modal.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>