我嘗試添加動態使用jQuery添加動態使用jQuery
此代碼,我成功地寫了創建下拉列表中可以根據需要,但是代碼渲染意外格式的HTML標籤,在這裏它把DropDownList的幫手MVC DROPDOWNLIST HTML助手每個屬性值的雙引號! (如姓名=「」 HousingUnitCount「」)
<select class=""form-control" input-sm"="" id=""HousingUnitCount"" name=""HousingUnitCount""><option value="""">Choose the number of housing Unit</option>
<option value=""0"">0</option>
<option value=""1"">1</option>
<option value=""2"">2</option>
<option value=""3"">3</option>
<option value=""4"">4</option>
<option value=""5"">5</option>
</select>
下面是代碼我有,
這裏我定義爲我的列表和HTML輔助全局變量jQuery的
@{
ViewBag.Title = "TestingDynamicFormWithjQuery";
var unitsCountList = new List<SelectListItem> { new SelectListItem() {Text="0", Value="0"},
new SelectListItem() {Text="1", Value="1"},
new SelectListItem() {Text="2", Value="2"},
new SelectListItem() {Text="3", Value="3"},
new SelectListItem() {Text="4", Value="4"},
new SelectListItem() {Text="5", Value="5"}
};
var housingUnit = Html.DropDownList("HousingUnitCount", unitsCountList, "Choose the number of housing Unit", htmlAttributes: new { @class = "form-control input-sm" }).ToHtmlString();
}
使用
這是我使用
<button id="addUnitBtn" type="button" name="addUnitBtn" class="btn btn-primary">
<span class="glyphicon glyphicon-plus-sign"></span> Add Unit
</button>
@using(Html.BeginForm())
{
<div id="addUnitDiv">
</div>
<br/><br /><br />
<input id="Submit" type="submit" value="Save" class="btn btn-primary btn-lg" />
}
下面是jQuery的代碼中添加的DropDownList dynamicall的HTML Ÿ
@section Scripts{
<script type="text/javascript">
$(window).ready(function() {
console.log('windows ready');
$('button[name="addUnitBtn"]').on("click", function() {
console.log('btn clicked');
$("#addUnitDiv").append('<div>@Ajax.JavaScriptStringEncode(housingUnit)</div>');
});
});
</script>
}
'JavaScriptStringEncode'將編碼字符串。你需要對它進行編碼嗎? – luke2012 2014-10-09 21:37:14
那麼我怎樣才能在JavaScript代碼中嵌入一個HTML助手而不使用JavaScriptStringEncode?我試圖嵌入mvc幫手而不使用JavaScriptStringEncode,它不起作用:( – SaDHacK 2014-10-09 22:37:02