打开后如何单击模态内的按钮?

时间:2016-06-30 14:07:44

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 bootstrap-modal

我真的想知道,在点击链接打开后,如何点击Bootstrap模式中的按钮:

<a href="#" data-toggle="modal" data-target="#myid">

按钮有'id',所以我想我可以点击按钮使用 js ,但我真的不知道如何处理这个。

我可以在模态调用后的$("#buttonid").click();中使用data-target等函数吗?

我试过没有结果。 任何帮助将不胜感激

这是按钮代码:

<button type="submit" id="buttonid" name="Profile" href="#">

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
$('#myModal').on('shown.bs.modal', function (event) {
 $("#newBtn").trigger("click");  
});

 $("#newBtn").on("click",function(){
 alert("button inside modal clicked");
 
 })
&#13;
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- 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">
   You can make the button hidden by adding class hidden to button.
       <button type="button" id="newBtn" class="btn btn-sm btn-info">clicked on modal open</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在显示模态的事件

上调用触发器
$('#myid').on('shown.bs.modal', function () {
   $("#buttonid").trigger('click');
});