[英]Jquery append MVC PartialView with scripts
I have javascript function which appends PartialView
into div.我有将
PartialView
附加到 div 的 javascript 函数。 Everything works fine, except that PartialView contains some <script></script>
scripts.一切正常,除了 PartialView 包含一些
<script></script>
脚本。 And those dynamically appended functions are not found by jQuery - Uncaught ReferenceError: getIDc4 is not defined
. jQuery 找不到那些动态附加的函数 -
Uncaught ReferenceError: getIDc4 is not defined
。
My JS function我的JS函数
var url = '@UrlHelper.GetFullUrl("Inventories/GetFilterRow")'; //this returns partial view
$.ajax({
type: 'POST',
data: { aRef: aRef },
url: url,
success: function(data) {
$('.filter').append($(data)[0]);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
}
});
and partial view和局部视图
...
@{
int id = Model != null ? Model.Id : 1;
}
<div id="@Html.Raw("FilterCondition" + id)">
...
...
...
@{
string getIdcFunction = string.Format("getIDc{0}", id);
}
<script>
@{
@:function
@getIdcFunction<text>() {
var grid = $("@Html.Raw("#field"+id)").data("kendoDropDownList");
var item = grid.value();
return { IDc: item };
}
</text>
}
</script>
</div>
How can I get those dynamically appended functions get working?我怎样才能让那些动态附加的函数开始工作?
When I generate the partial view by using on page loading with当我通过使用页面加载生成局部视图时
@Html.Partial("GetFilterRow", new {Id = 1})
everything works fine.一切正常。 The only problem is when this view is appendend via jquery and ajax.
唯一的问题是当这个视图通过 jquery 和 ajax 追加时。 Then those functions are not found.
然后找不到那些函数。
Thanks for help.感谢帮助。
You have a couple options that I am aware of.你有几个我知道的选项。 Option one is to include your script in the main view and set the
onclick
or whichever event you're trying to call to call that function.选项一是将您的脚本包含在主视图中,并设置
onclick
或您尝试调用的任何事件来调用该函数。
For example, in your main view have:例如,在您的主视图中有:
function floorSelect(clickedFloor) {
doStuff;
}
And in your partial view have:在您的部分观点中,有:
<ul class="dropdown-menu" role="menu" aria-labeledby="dropDownSelectFloor">
@For i As Integer = 0 To Model.ListOfFloors.Count - 1
@<li role="presentation"><a id="ddListSelectFloor" class="ddListSelectFloor"
onclick="floorSelect(this)" role="menuitem" tabindex="-1">
@Model.ListOfFloors(i)</a>
</li>
Next
</ul>
Notice the onClick
which calls the script from the main view.请注意从主视图调用脚本的
onClick
。
Option number 2 is to attach the script via id
(you can probably do this to the class
as well)选项 2 是通过
id
附加脚本(您也可以对class
执行此操作)
Like this:像这样:
@For i As Integer = 0 To Model.ListOfDisplayTypes.Count - 1
If Model.ListOfDisplayTypes(i).Equals("DatePicker") Then
@<div class="row">
<div class="col-md-4">
<b>@Model.ListOfFields(i): </b>
</div>
<div class="col-md-4">
<input class="search" id="@Model.ListOfCategoryAttributeIDs(i)" type="text" placeholder="@Model.ListOfFields(i)">
</div>
<script>
jQuery("input[id='@Model.ListOfCategoryAttributeIDs(i)']").datepicker();
</script>
</div>
End If
Next
1.Put the scripts in the partial view into scripts.js file. 1.将局部视图中的脚本放入scripts.js文件中。 2.After you append the partial view into the div use
$.getscripts('path of scripts.js')
. 2.在将局部视图附加到 div 后,使用
$.getscripts('path of scripts.js')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.