简体   繁体   English

如何在while循环中将唯一数据传递给引导模式

[英]How to pass unique data to bootstrap modal in while loop

I am trying to create a delete-button for each printed row in a php while loop that opens a modal window. 我正在尝试为php while循环中的每个打印行创建一个删除按钮,以打开模式窗口。 The delete query will be executed from the modal. 删除查询将从模态中执行。

How can I pass the corresponding row's id, title etc to the modal? 如何将相应行的ID,标题等传递给模式? The way it is now, I only get the data from the first table row no matter what delete-button I press. 现在,无论我按什么删除按钮,我都只能从表的第一行获取数据。

There are many solutions out there, but I can't make them work. 有很多解决方案,但是我无法使它们起作用。 Hoping someone can help me understand this... 希望有人可以帮助我理解这一点...

PHP is server side, so it really needs done in Javascript. PHP是服务器端的,因此它确实需要用Javascript完成。

I created a Javascript lib if you are interested called bs-delete-confirm. 如果您有兴趣,我创建了一个Javascript库,称为bs-delete-confirm。 you add a class to your delete button, and when you click it, it stops an href from leaving the page and brings up a Bootstrap modal asking if you are sure you wish to perform the action. 您将一个类添加到“删除”按钮,然后单击该类,它将阻止href离开页面并显示Bootstrap模式,询问您是否确定要执行该操作。 Clicking ok follows the link, clicking cancel doesn't. 单击确定会跟随链接,而不会单击取消。 Simple!. 简单!。

Check it out here https://github.com/delboy1978uk/bs-delete-confirm 在这里查看它https://github.com/delboy1978uk/bs-delete-confirm

<a href="/delete-url" class="delete-class">X</a>

$(document).ready(function(){
  $('.delete_class').deleteConfirm();
});

and that's it! 就是这样! you now have a delete confirm modal :-) 您现在有一个删除确认模式:-)

I tend to create a hidden modal in the bottom of my html and clone it into a new variable, then change the data for that new modal, append it and fire it up. 我倾向于在html的底部创建一个隐藏的模态,并将其克隆到一个新变量中,然后更改该新模态的数据,将其追加并启动。

That way when you delete it or need to create a second one, you always have access to your template/blueprint. 这样,当您删除它或需要创建第二个模板时,您始终可以访问模板/蓝图。 It also allows you to keep a separate reference to every modal for later use. 它还允许您为每个模式保留单独的引用,以供以后使用。

eg $modal1.find('.inputEmail') and $modal2.find('.inputEmail') 例如$modal1.find('.inputEmail')$modal2.find('.inputEmail')

You can even make that dynamic afterwards if you have many inputs 如果您有很多输入,您甚至可以在以后进行动态处理

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

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