简体   繁体   English

jQuery事件未在加载的Bootstrap模态窗口中绑定

[英]jQuery events are not bind in loaded Bootstrap modal window

Trying to load dynamical content into Bootstrap modal with this code: 尝试使用以下代码将动态内容加载到Bootstrap模式中:

$('body').on('click', '.time-estimation', function () {
    var taskId = $(this).data("task_id");
    $("#modal-content").load("/tasks/estimation?task_id=" + taskId, function () {
        $('#myModal').modal({show: true, keyboard: true, backdrop: true});
    });
});

it works and modal window is displayed. 它可以工作并显示模式窗口。 But in loaded content there is another jQuery code, that is not binded: 但是在加载的内容中,还有另一个未绑定的jQuery代码:

 $(document).ready(function () {
     $("#TaskEstimationTimeForm").submit(function (event) {
         event.preventDefault();
    });
});

Why is not triggered this submit event? 为什么不触发此submit事件?

$(document).ready is only triggered when a document is initially loaded in a window or iframe. 仅当文档最初加载到窗口或iframe中时,才触发$(document).ready In your case you're pulling HTML directly in an element, and that even will not be fired. 在您的情况下,您是直接在元素中提取HTML,甚至不会被触发。 You don't need it though, you should just be able to do: 虽然您不需要它,但是您应该能够:

$("#TaskEstimationTimeForm").submit(function(event) {
    event.preventDefault();
});

You can do this as it is working for me 您可以这样做,因为它对我有用

$('#myModal').modal({show: true, keyboard: true, backdrop: true})
.one('submit','#TaskEstimationTimeForm',function(){
  //your stuff on event here
})

Hope it helps 希望能帮助到你

Thanks 谢谢

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

相关问题 当引导模式 window 完全加载而没有出现异步问题时,如何加载 DataTable jQuery 插件? - How to load the DataTable jQuery plugin when a Bootstrap modal window is completely loaded without getting asynchronous issues? 使用jQuery检测引导程序加载的图像 - Detect images loaded in bootstrap modal with jQuery 在模态窗口Bootstrap / jQuery上显示表单条目 - Showing Form Entry on Modal Window Bootstrap / jQuery jQuery imgAreaSelect在引导模式窗口中不起作用 - jquery imgAreaSelect not working in bootstrap modal window 如何使用 jQuery 打开 Bootstrap 模式 window? - How to open a Bootstrap modal window using jQuery? 是否有可能在不使用加载调用的情况下将表单绑定到模态引导程序 window? - Is there a possibility to bind the Form to a modal bootstrap window without using the load call? Bootstrap JavaScript动态添加事件而不链接到模式窗口 - Bootstrap JavaScript adding events dynamically not linking to modal window 如何在没有 jQuery 的情况下将播放事件绑定到 AJAX 加载的视频? - How to bind play events to AJAX loaded videos without jQuery? jQuery - 如果/在 window 上调整大小(如何在 jQuery 中绑定 2 个或更多事件) - jQuery - if / else on window resize (how to bind 2 or more events in jQuery) Bootstrap 4 Ajax加载模式 - Bootstrap 4 ajax loaded modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM