我试图在 MVC2 View 页面中单击时调用 JavaScript 函数。
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>,<%=o.ba_Object_Code %>)" href="#">Select</a>
JavaScript 函数
function SelectBenefit(id,code) {
alert(id);
alert(code);
}
这里的 ba_Object_Id 和 Code 是来自 ViewModel 的值。如果我使用 SelectBenefit(<%=o.ba_Object_id %>)
这样,它工作正常。但是当我有两个参数时它不是。我收到这个错误:
conditional compilation is turned off.
最佳答案
我认为如果第二个参数是一个字符串,你需要用引号引起来:
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>, '<%=o.ba_Object_Code %>')" href="#">Select</a>
据说您的参数需要正确编码,我不会像这样传递它们。我会将它们序列化为 JSON 对象以确保一切正常。像这样:
<a onclick="SelectBenefit(<%= new JavaScriptSerializer().Serialize(new { id = o.ba_Object_id, code = o.ba_Object_Code }) %>)" href="#">Select</a>
然后 SelectBenefit 函数可能如下所示:
function SelectBenefit(benefit) {
alert(benefit.id);
alert(benefit.code);
}
关于c# - JavaScript 错误 : conditional compilation is turned off in MVC2 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686283/