[英]JQuery Append actionlink to div
//code
$(document).on("click", ".open-chartModal", function () {
var spNames = $(this).data('id');
$("#spNames").val(spNames);
var separated = spNames.split(";");
for (var i = 0, length = separated.length; i < length; i++) {
var chunk = separated[i];
$('#options').append('<li>@Ajax.ActionLink("Chart", "Chart",
new { spName = "'+ chunk +'" }, new AjaxOptions { }) </li>')
}
});
The above is reading 'chunk' as string and not a variable. 上面的代码将“块”读取为字符串而不是变量。 I want to put spNames = var chunk. 我想把spNames = var块。 Do not know if the single quotes are in correct place. 不知道单引号是否在正确的位置。
Try this 尝试这个
var url = '@Ajax.ActionLink("Chart", "Chart", new { spName = "-1" }, new AjaxOptions { })';
$('#options').append('<li>' + url.replace('-1', chunk )+ '</li>')
As @Ajax.ActionLink
will be rendered by razor. 由于@Ajax.ActionLink
将由razor呈现。 You can't pass JavaScript variables. 您不能传递JavaScript变量。
You can add an placeholder value & replace it with your variable. 您可以添加占位符值并将其替换为变量。
I think you want this: 我想你想要这个:
for (var i = 0, length = separated.length; i < length; i++) {
var chunk = separated[i];
$('#options').append('<li>@Ajax.ActionLink("Chart", "Chart",' +
'new { spName = "'+ chunk +'" }, new AjaxOptions { }) </li>')
}
Your newline broke your string literal. 您的换行符破坏了您的字符串文字。 Need to close it and explicitly concatenate. 需要关闭它并显式连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.