我有两个自定义的javascript文件(viewJscript.js和partialJscript.js)。在视图中我简单地说:
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/viewJscript.js"></script>
这很好用。现在我试图在我的局部视图中执行相同的操作,该视图嵌套在视图中:
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/partialJscript.js"></script>
它不起作用。 那么我应该如何在部分视图中加载partialJscript.js呢?
我忘记提到部分视图是动态的,并且在从视图中单击按钮时打开
编辑这是我在partialJscript.js文件中的代码:
$(function () {
var divSensorNames = $("#sensorNames");
$('.webgrid2-table th:first-child, .webgrid2-table td:first-child').hide();
$('.webgrid2-table th:nth-child(2), .webgrid2-table td:nth-child(2)').hide();
$('.webgrid2-table th:nth-child(3), .webgrid2-table td:nth-child(3)').hide();
$('#AddNewSensor').on('click', function () {
$('#sensorList').load('@Url.Action("ChooseSensorList", "PredefinedViews")');
});
$('.editSensorMode').on("click", function () {
var tr = $(this).parents('tr:first');
//var SensorID = tr.find("#SensorID").html();
var PredefinedViewItemID = tr.find("#PredefineViewsItemID").html();
var urlEditModes = "@Url.Action("EditSensorMode", "PredefinedViews", new { pviId = "PredefinedViewItemID" })";
//urlEditModes = urlEditModes.replace("SensorID", SensorID);
urlEditModes = urlEditModes.replace("PredefinedViewItemID", PredefinedViewItemID);
$('#sensorList').load(urlEditModes);
});
$(".sensor-delete-table").on("click", function () {
var tr = $(this).parents('tr:first');
var PredefineViewsItemID = tr.find("#PredefineViewsItemID").html();
var PredefineViewID = tr.find("#PredefineViewID").html();
var amount = parseInt($("[data-id='" + PredefineViewID + "']").text());
//amount = amount + anyNumber; This also works!
//var rowCountBefore = $('.webgrid2-table tr').length;
var flag = confirm('@Html.Localize("deleteCheck")');
var urlShowNewSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefineViewID" })";
urlShowNewSensors = urlShowNewSensors.replace("PredefineViewID", PredefineViewID);
if (PredefineViewID != "" && flag) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: '@Url.Action("DeleteSensor", "PredefinedViews")',
data: JSON.stringify({ pviID: PredefineViewsItemID, pID: PredefineViewID }),
dataType: "json",
complete: function (result) {
var urlShowSensors = "@Url.Action("ShowSensorNames", "PredefinedViews", new { predefinedViewID = "PredefID" })";
urlShowSensors = urlShowSensors.replace("PredefID", PredefineViewID);
$(divSensorNames).load(urlShowSensors);
amount--;
$("[data-id='" + PredefineViewID + "']").text(amount.toString());
});
}
});
});
答案 0 :(得分:2)
换句话说,您正在使用AJAX引入局部视图的HTML。出于安全原因,浏览器不允许运行动态插入HTML的脚本标记。您必须在主视图中包含脚本,或者在AJAX回调中通过JavaScript动态加载脚本。您可以使用Require.js或Modernizr.load。
这样做