简体   繁体   English

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

[英]How to click a button inside a modal after opens it?

I really want to know, how can I click a button inside a Bootstrap modal after I've open it by clicking its link: 我真的很想知道,在我通过单击其链接来打开Bootstrap模态后如何单击它:

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

Buttons have ids', so I think I can click the button using js , but I don't really know how to deal with this. 按钮有id',所以我想我可以使用js单击按钮,但是我真的不知道如何处理。

Could I use a function such as $("#buttonid").click(); 我可以使用$("#buttonid").click();的函数$("#buttonid").click(); in the data-target after the modal call? 模态调用后在data-target

I tried with no results. 我尝试没有结果。 Any help would be appreciate 任何帮助将不胜感激

Here is the button code: 这是按钮代码:

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

 $('#myModal').on('shown.bs.modal', function (event) { $("#newBtn").trigger("click"); }); $("#newBtn").on("click",function(){ alert("button inside modal clicked"); }) 
 <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> 

Call the trigger on the modal shown event 在模态显示事件上调用触发器

$('#myid').on('shown.bs.modal', function () {
   $("#buttonid").trigger('click');
});    

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

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