不要运行PartialView javascript函数 [英] Don't run javascript function in PartialView
本文介绍了不要运行PartialView javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我母版添加一个js文件。在视图中使用它。
< DIV CLASS =控制组>
@ Html.LabelFor(型号=> model.Computers,新{@class =控制标签})
< DIV ID =computersEditorRows的风格=明确:既;保证金右:30PX;填充:10px的;边界:1px的固体RGB(204,204,204);>
@foreach(在Model.Computers VAR项)
{
Html.RenderPartial(_ ComputersEditorRow项目);
} < / DIV>
<一个ID =addItemcomputer的风格=光标:指针;>的AddItem< / A>
< / DIV>
和js文件
$(文件)。就绪(函数(){
$(#addItemcomputer)。点击(函数(){
$获得(/ TechnicalOfficerService / AddComputerNewRow功能(数据){
$(#computersEditorRows)追加(数据)。 })失败(函数(XHR,ERR){
警报(readyState的:+ xhr.readyState +\\ n状态:+ xhr.status);
警报(responseText的:+ xhr.responseText);
});
});
})
但是,当我在Partialview用这个,不调用函数。我使用局部视图这个网站code,但不叫 $(#addItemcomputer)。点击
。
管窥
@model PSYCO.Web.Sepid.ViewModels.ComputerViewModel
@using PSYCO.Web.Sepid.Helpers;
< DIV CLASS =控制组>
@using(Html.BeginCollectionItem(计算机))
{
< DIV CLASS =控制组>
@ Html.LabelFor(型号=> model.Computers,新{@class =控制标签})
< DIV ID =computersEditorRows的风格=明确:既;保证金右:30PX;填充:10px的;边界:1px的固体RGB(204,204,204);>
@foreach(在Model.Computers VAR项)
{
Html.RenderPartial(_ ComputersEditorRow项目);
} < / DIV>
<一个ID =addItemcomputer的风格=光标:指针;>添加项目< / A>
< / DIV>
<一类=removeItemNationality的风格=光标:指针;>删除< / A>
}
< / DIV>
修改
添加本节的局部视图。
@section脚本
{
<脚本>
$(文件)。在(点击,#addItemcomputer功能(){
警报('D');
})
< / SCRIPT>
}
解决方案
应该PartialView添加脚本。
使用脚本局部视图链接。
<脚本类型=文/ JavaScript的SRC =@ Url.Content(/脚本/ SomeScript.js)>< / SCRIPT>
或者局部图使用的脚本。(不能用在局部视图部分)
<脚本>
$(#addItemcomputer)。点击(函数(){
$获得(/ TechnicalOfficerService / AddComputerNewRow功能(数据){
$(#computersEditorRows)追加(数据)。})失败(函数(XHR,ERR){
警报(readyState的:+ xhr.readyState +\\ n状态:+ xhr.status);
警报(responseText的:+ xhr.responseText);
});
});
< / SCRIPT>
I add a js file in masterpage. and use it in 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>
and js file
$(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);
});
});
})
But when i use this in Partialview, don't call function. I use this html code in partial view, but don't call $("#addItemcomputer").click
.
Partial View
@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>
EDIT
Add this section in partial view.
@section Scripts
{
<script>
$(document).on("click","#addItemcomputer",function () {
alert('d');
})
</script>
}
解决方案
Should add script in PartialView.
Use script link in Partial View.
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
Or use script tag in Partial View.(cannot use sections in partial views)
<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>
这篇关于不要运行PartialView javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文