在我的 ASP.NET Core 6 MVC 应用程序中,我尝试动态创建 jQuery 函数名称。
我在控制器中的代码是这样的:
string int= 1 // sid is a variable
string fn = "function test_+{sid}() {
var params = 'top = 0, left = 0';params +='width =' + screen.width + ', height =' + screen.height + ',minimizable = no,statusbar = no,toolbar = no,location = no,directories = no,menubar = no,resizable = no,copyhistory = no';
params += ', scrollbars = yes, status = no, fullscreen = yes';
var url =document.URL;
newwin = window.open('/user/instruction?p1=" + sid + @"&p2=" + dr["DifficultyLevel"].ToString() + @"&s1=" + 'd' + @"&p3=" + dr["DifficultyLevel"].ToString() + @", _blank, params');"
这里
sid
在循环内运行。 sid
需要动态更改,并将添加函数名称test_
作为后修复。我已经尝试过 {}
但它不起作用。
通过在每行末尾添加简单的 + 即可实现这一点。请找到下面的示例代码,可以用作任何控制器中的操作方法进行测试。
public string Index()
{
List<string> functionNames = new List<string>();
for (int i = 0; i < 10; i++)
{
var name = Guid.NewGuid().ToString();
var fn = @"function test_" + name + "}(){" + //Add + at the end of the each line of js function
"console.log('" + DateTime.Now.ToString() + "');" +
"} ";
functionNames.Add(fn);
}
return string.Join("----", functionNames);
}