<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Username or Password is incorrect.</div>
<div class="modal-footer">
<a class="btn btn-primary" href="login.html">Ok</a>
</div>
</div>
</div>
</div>
所以我有一个当前是一个按钮的标签。当我单击此按钮时,它会运行一条显示消息。
如何使此标记在页面打开或重新加载时自动运行。
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">Logout</a>;
发布于 2019-03-18 16:26:18
click()方法在元素上执行一次单击,就像用户手动单击它一样。
在你的代码中添加id:
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal" id="example">Logout</a>并使用js:
document.getElementById('example').click();发布于 2019-03-18 16:23:26
您可以在页面加载后使用触发器
$( document ).ready(function() {
$(".dropdown-item").trigger( "click" );
});发布于 2019-03-18 16:55:27
基于你上面的评论...
我正在尝试做一个表单,如果用户输入错误的凭据,模式将弹出。
...maybe这会有帮助的。如果不是,那么你需要在你的问题中澄清你想要做什么。
在下面的代码片段中,我有一个登录表单。当输入了错误的凭据时,将出现模式。
$("#login").click(function () {
// Here you would do your check for correct credentials
// Obviously you wouldn't want to do this in JS in production
// I'm assuming there would be an AJAX request here that would validate the user's credentials
// If valid, proceed. If not, call the modal.
if ($(username).val() !== "blah" || $(password).val() !== "blah") {
// Username or password incorrect, show modal
$("#modal").modal();
} else {
alert("Login successful!");
}
})<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
Username: <input id="username" type="text" />
Password: <input id="password" type="password" />
<button type="button" id="login" class="btn btn-info">Login</button>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Username or Password is incorrect.</div>
<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal">Try again</a>
</div>
</div>
</div>
</div>
基本上,任何时候你想让模式自动出现(也就是,不用点击任何东西),就像这样调用它:
$("#idOfModal").modal();如果您想以编程方式隐藏或取消模态,可以通过将hide传递给modal函数来实现:
$("#idOfModal').modal("hide");https://stackoverflow.com/questions/55217095
复制相似问题