简体   繁体   English

JavaScript / jQuery在Ajax加载的编辑器/显示模板上不起作用

[英]JavaScript/jQuery doesn't work on Editor/Display templates loaded with Ajax

I created editor and display templates for my entire object downline. 我为整个对象下线创建了编辑器和显示模板。

Now one of the inner objects' text boxes should be a date-picker. 现在,内部对象的文本框之一应该是日期选择器。

The thing is it's loaded after the initial loading using Ajax , and the included scripts don't work. 问题是它是在使用Ajax进行初始加载之后加载的,并且所包含的脚本不起作用。

I tried to include the following script on the bottom of the template, the script renders but does nothing: 我试图在模板的底部包括以下脚本,该脚本呈现但不执行任何操作:

@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/jquerytimepicker")

<script type="text/javascript">
  alert('dddddddddddddddddddddddddd');
  $('.time-picker').timepicker();
</script>

In the code abve I also added a dummy alert just to prove that the script isn't running, which makes the question much simpler: "How to run scripts from templates and partial views?". 在上面的代码中,我还添加了一个虚拟警报,只是为了证明脚本未在运行,这使问题变得更加简单:“如何从模板和局部视图运行脚本?”。

I tried to include the scripts in a section define in the outer page, but that doesn't work, as templates and partial view cannot include sections by design ( Read this ). 我试图将脚本包括在外页中定义的部分中,但这不起作用,因为模板和部分视图无法按设计包括部分(请阅读本文 )。

it doesn't work because the js that attaches the timepicker to the html runs at document.ready, but the content loaded via ajax is loaded after document.ready 它不起作用,因为将时间选择器附加到html的js在document.ready上运行,但是通过ajax加载的内容在document.ready之后加载

so you need to call the method that attaches the timepicker again after the ajax request has completed 所以您需要在ajax请求完成后再次调用附加时间选择器的方法

you can try this: 您可以尝试以下方法:

$(document).ajaxComplete(function(event, xhr, settings) {
    alert('an ajax request completed');
    $('.time-picker').timepicker();
});

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

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