partialView 中的 JS 从未触发
JS in partialView never fired
我有一个包含一些 JS 的局部视图,但它似乎从未被触发:
我的部分:
@model List<String>
@if (Model != null)
{
foreach (var item in Model)
{
<button id="application-@item" type="button" class="btn btn-default" onclick="showUses('@item')">@item</button>
}
}
@section scripts
{
<script type="text/javascript">
$(function () {
alert("plop");
});
</script>
}
主视图:
...
<div class="col-md-6 ">
<h2>Applications</h2>
<div id="Application" class="btn-group-vertical" role="group">
@Html.Partial("_ApplicationPartial", null, new ViewDataDictionary())
</div>
</div>
...
@section scripts
{
<script type="text/javascript">
$(function () {
$('[id^=univers]').click(function () {
var selectedButton = $(this).attr('id');
var selectedUniverse = selectedButton.substring(selectedButton.indexOf('-') + 1, selectedButton.lenght);
$("#Application").load('@(Url.Action("Application", "UseAndNeed", null, Request.Url.Scheme))?idUniverse=' + selectedUniverse);
});
});
</script>
}
应该在加载页面时以及在我使用 load() 刷新 partialView 内容时触发警报。
这是正常行为吗,JS 永远不会在 partialView 中触发,或者我错过了什么?
在部分视图中 document.ready()
不会触发。
您可以通过以下方式完成
$.ajax({
type:'get',
url:'/.....',
cache:false,
success:function(){/* Set your records here. Use the returns view hidden variables. */....},
complete:function(){ /*Call your partial view functions here */ }
});
调用成功后,您可以填充部分网格。
喜欢
$('#selector').html(Response);
我有一个包含一些 JS 的局部视图,但它似乎从未被触发:
我的部分:
@model List<String>
@if (Model != null)
{
foreach (var item in Model)
{
<button id="application-@item" type="button" class="btn btn-default" onclick="showUses('@item')">@item</button>
}
}
@section scripts
{
<script type="text/javascript">
$(function () {
alert("plop");
});
</script>
}
主视图:
...
<div class="col-md-6 ">
<h2>Applications</h2>
<div id="Application" class="btn-group-vertical" role="group">
@Html.Partial("_ApplicationPartial", null, new ViewDataDictionary())
</div>
</div>
...
@section scripts
{
<script type="text/javascript">
$(function () {
$('[id^=univers]').click(function () {
var selectedButton = $(this).attr('id');
var selectedUniverse = selectedButton.substring(selectedButton.indexOf('-') + 1, selectedButton.lenght);
$("#Application").load('@(Url.Action("Application", "UseAndNeed", null, Request.Url.Scheme))?idUniverse=' + selectedUniverse);
});
});
</script>
}
应该在加载页面时以及在我使用 load() 刷新 partialView 内容时触发警报。
这是正常行为吗,JS 永远不会在 partialView 中触发,或者我错过了什么?
在部分视图中 document.ready()
不会触发。
您可以通过以下方式完成
$.ajax({
type:'get',
url:'/.....',
cache:false,
success:function(){/* Set your records here. Use the returns view hidden variables. */....},
complete:function(){ /*Call your partial view functions here */ }
});
调用成功后,您可以填充部分网格。
喜欢
$('#selector').html(Response);