简体   繁体   English

单击按钮填充Bootstrap 3模态

[英]Populating Bootstrap 3 Modal on Button Click

I am a junior web developer and am currently creating a dynamic website. 我是一名初级Web开发人员,目前正在创建一个动态网站。 Within this website I am using Javascript in order to structure my pages eg entering HTML into a var string and calling upon it to load on the page when necessary. 在该网站中,我使用Javascript来构造我的页面,例如将HTML输入到var字符串中,并在必要时调用它以将其加载到页面上。

I am trying to use the Bootstrap Modal for various forms to input data into my database. 我正在尝试将Bootstrap Modal用于各种形式,以将数据输入到数据库中。 This is giving me some trouble as some elements such as datepicker is not working I assume because the Modal loads on click of the button where as the date picker loads on page load. 这给我带来了一些麻烦,因为我认为某些元素(例如datepicker)无法正常工作,因为在单击按钮时(当日期选择器在页面加载时)加载了Modal。

Anyway, I just wanted to know if there is a way to have content and my custom Javascript to load when the bootstrap modal button is clicked eg 无论如何,我只是想知道单击引导程序模式按钮时是否有一种方法可以加载内容和自定义Javascript,例如

        var outputContainer1 = $('#selectCraft');
$.ajax(apiPrefix + '/craft' + apiSuffix, {
    error: function() {},
    success: function(data){
        for(var i in data){
            $('#selectCraft').append('<option value="'+ data[i].id +'">'+data[i].aircraft_name+'</option>');
        }
    },
    type: 'GET'
});

As you can see this is a select element that is getting its information from the database. 如您所见,这是一个select元素,正在从数据库中获取其信息。 Which I want to load on button click rather then at the page-load. 我想在按钮单击而不是页面加载时加载。

My data target for the button is "#flightModal" and the Modal ID is "#flightModal". 我的按钮数据目标是“ #flightModal”,模态ID是“ #flightModal”。

If anyone can help I would much appreciate. 如果有人可以提供帮助,我将不胜感激。

Thanks 谢谢

Ibrahim 易卜拉欣

You just need to initialize your datepicker after the modal is created. 创建模态 ,您只需要初始化日期选择器即可。 So build you DOM in the success callback, then initiaze the datepicker as you would on a normal page load: 因此,请在success回调中构建DOM,然后像在普通页面加载中一样初始化datepicker:

success: function () {
    // ...
    $('#datepicker').datetimepicker();
}

By the way, if you are working with modals, I recommend using the Bootstrap Dialog JavaScript library, it makes working with models programmatically much easier. 顺便说一句,如果您正在使用模态,我建议您使用Bootstrap Dialog JavaScript库,该库使以编程方式使用模型更加容易。 For example you can show a simple modal window like this: 例如,您可以显示一个简单的模态窗口,如下所示:

BootstrapDialog.show({
    title: 'Hello World',
    message: 'This is a hello world message'
});

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

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