[英]Bootstrap Popover AND a Modal (hover and click)
Scenario: user profile. 场景:用户个人资料。 I would like to be able to display a user name with a popover that displays a limited amount of information from the user profile.
我希望能够通过弹出窗口显示用户名,该弹出窗口显示来自用户个人资料的有限信息。 So far, I have that part working.
到目前为止,我已经开始工作了。 I can build it on the fly and have it do what I need.
我可以即时构建它,让它做我需要的事情。 The popover works perfectly.
弹窗效果很好。
What I would also like to do is have the user be able to click on the user name and bring up a Bootstrap modal form with more information about the user (if provided). 我还想做的是让用户能够单击用户名,并显示一个Bootstrap模态表单,其中包含有关用户的更多信息(如果提供)。 The first problem I am seeing is that it appears the data-toggle attribute can only have a single setting:
我看到的第一个问题是看来data-toggle属性只能有一个设置:
echo '<a href="#" class="trigger userprof" data-toggle="popover" data-target="#userModal" data-popover-content="#content' . $user_row['user_id'] . '">' . $user_row['user_name'] . '</a>';
In that example, if I add the modal to the data-toggle attribute it doesn't seem to do me much good. 在该示例中,如果我将模式添加到data-toggle属性,则似乎对我没有多大帮助。
I have discovered by tinkering (and that is why the class 'userprof' in the code above), that a JavaScript click event can be triggered (right now all I'm doing is a basic JS alert dialog to test), but from there I would want to load the modal. 我通过修补(这就是上面代码中的类'userprof'的原因)发现,可以触发JavaScript click事件(现在我正在做的只是一个基本的JS警告对话框进行测试),但是从那开始我想加载模态。 I am not sure if I can make it all work.
我不确定是否可以全部使用。
I have a set of functions I've used successfully for another modal (calling this one 'userModal') that I got some help from someone here a while back with -- is it possible to call that from the click event? 我有一套已经成功用于另一种模式(称为“ userModal”)的函数,我从这里的某人那里得到了一些帮助-可以从click事件中调用它吗?
// code to open the modal with the caption and description:
$('#userModal').on('show.bs.modal', function (event)
{
var button = $(event.relatedTarget); // Button that triggered the modal
var title = button.data('title'); // Extract info from data-* attributes
var body = button.data('body'); // Extract info from data-* attributes
var modal = $(this);
modal.find('.modal-title').text( title );
modal.find('.modal-body').append( body );
});
// when modal closes, clear out the body:
$('#userModal').on('hidden.bs.modal', function ()
{
$(this).find(".modal-body").text('');
});
Since these are "anonymous" functions I am not sure I can call them ... feeling a bit lost in the code here. 由于这些是“匿名”函数,因此我不确定是否可以调用它们……在这里的代码中感觉有点迷茫。 Any help pointing me in the right direction would be great.
向我指出正确方向的任何帮助都会很棒。 I'd even be willing to consider a different idea, but I would like this kind of functionality (hover and click) for this situation and possibly something else.
我什至愿意考虑一个不同的想法,但是我想针对这种情况(可能还有其他事情)使用这种功能(悬停并单击) 。 Thanks!
谢谢!
You can try: 你可以试试:
$(document).on('click', 'a.userprof', function(){
$('#userModal').modal('show');
});
To make your callback function work, you need to add according data-* attribute to each of the <a>
tag. 为了使回调函数正常工作,您需要向每个
<a>
标记添加相应的data- *属性。
You're listening for the modal to show itself, when the DOM is showing the modal. 当DOM显示模式时,您正在侦听模式显示。
try using something like this, and use a button or a link with data-toggle="modal"
尝试使用类似的方法,并使用带有
data-toggle="modal"
的按钮或链接
$(document).on('hidden.bs.modal', '#userModal', function ()
{
$(this).find(".modal-body").text('');
});
for reference https://jsfiddle.net/y063mu4t/1/ 供参考https://jsfiddle.net/y063mu4t/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.