[英]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.