我在一个 asp.net c# 项目中。我想打印带有条形码的员工卡。因此,我在字符串变量调用 htmlCon
中编写一个 html 标签,它将在客户端绑定(bind)一个 DIV 标签(获取所有员工并循环打印)。
它工作正常。但在 htmlCon
变量内部有 java 脚本函数,它不会在循环中运行。
protected void btnGenarate_ClickEvent(object sender, EventArgs e)
{
......
foreach (var item in empDetails)
{
htmlCon += ..........+
"<script>"+
"$(document).ready(function ()"+
"{ $('#barcode').JsBarcode('"+ item.EmployeeNo + "', { width: 1, height: 30 }); });"+
"</script>" +
"<img id='barcode' class='barcode'/>" +
"........................................"+
}
}
此代码带有条形码,它将在循环中打印第一轮。我想运行所有员工以获取条形码。
最佳答案
您正在生成许多具有相同 ID 的图像,您应该为每次循环迭代生成一个新的 ID。我还建议使用 StringBuilder 而不是一堆字符串连接:
protected void btnGenarate_ClickEvent(object sender, EventArgs e)
{
StringBuilder htmlCon = new StringBuilder();
for (int i = 0; i < empDetails.Count; i++)
{
htmlCon.AppendFormat("<script>$(document).ready(function () { $('#barcode{0}').JsBarcode('{1}', { width: 1, height: 30 }); });</script><img id='barcode{0}' class='barcode'/>",
i.ToString(), empDetails[i].EmployeeNo);
htmlCon.Append("........................................");
}
//To Use StringBuilder value
string html = htmlCon.ToString();
}
关于C# asp.net 按钮单击事件中的 JavaScript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770793/