[英]Don't run javascript function in PartialView
我在母版页中添加一个js文件。 并在View中使用它。
<div class="control-group">
@Html.LabelFor(model => model.Computers, new { @class = "control-label" })
<div id="computersEditorRows" style="clear: both; margin-right: 30px; padding: 10px; border: 1px solid rgb(204, 204, 204);">
@foreach (var item in Model.Computers)
{
Html.RenderPartial("_ComputersEditorRow", item);
}
</div>
<a id="addItemcomputer" style="cursor: pointer;">AddItem</a>
</div>
和js文件
$(document).ready(function () {
$("#addItemcomputer").click(function () {
$.get("/TechnicalOfficerService/AddComputerNewRow", function (data) {
$("#computersEditorRows").append(data);
}).fail(function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});
})
但是当我在Partialview中使用它时,不要调用函数。 我在部分视图中使用此html代码,但不要调用$("#addItemcomputer").click
。
部分视图
@model PSYCO.Web.Sepid.ViewModels.ComputerViewModel
@using PSYCO.Web.Sepid.Helpers;
<div class="control-group">
@using (Html.BeginCollectionItem("Computers"))
{
<div class="control-group">
@Html.LabelFor(model => model.Computers, new { @class = "control-label" })
<div id="computersEditorRows" style="clear: both; margin-right: 30px; padding: 10px; border: 1px solid rgb(204, 204, 204);">
@foreach (var item in Model.Computers)
{
Html.RenderPartial("_ComputersEditorRow", item);
}
</div>
<a id="addItemcomputer" style="cursor: pointer;">Add Item</a>
</div>
<a class="removeItemNationality" style="cursor: pointer;">Delete</a>
}
</div>
编辑
在部分视图中添加此部分。
@section Scripts
{
<script>
$(document).on("click","#addItemcomputer",function () {
alert('d');
})
</script>
}
应该在PartialView中添加脚本。
在部分视图中使用脚本链接。
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
或在部分视图中使用脚本标签。(不能在部分视图中使用部分)
<script>
$("#addItemcomputer").click(function () {
$.get("/TechnicalOfficerService/AddComputerNewRow", function (data) {
$("#computersEditorRows").append(data);
}).fail(function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.