[英]JavaScript Error: conditional compilation is turned off in MVC2 View
I am trying to call a JavaScript function on click in a MVC2 View Page. 我试图在MVC2视图页面上单击调用JavaScript函数。
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>,<%=o.ba_Object_Code %>)" href="#">Select</a>
JavaScript function JavaScript函数
function SelectBenefit(id,code) {
alert(id);
alert(code);
}
Here ba_Object_Id and Code are the values from the ViewModel. 这里ba_Object_Id和Code是ViewModel中的值。 If I use
SelectBenefit(<%=o.ba_Object_id %>)
in this way, its working fine. 如果我以这种方式使用
SelectBenefit(<%=o.ba_Object_id %>)
,它的工作正常。 But when I have two paramaters its not.I am getting this error: 但是,当我有两个参数时,它没有。我收到了这个错误:
conditional compilation is turned off.
I think that you need to put quotes around the second parameter if it is a string: 我认为如果它是一个字符串,你需要在第二个参数周围加上引号:
<a onclick=" SelectBenefit(<%=o.ba_Object_id %>, '<%=o.ba_Object_Code %>')" href="#">Select</a>
This being said your parameters need to be properly encoded and I wouldn't pass them likse this. 这就是说你的参数需要正确编码,我不会通过它们喜欢这个。 I would serialize them as JSON object to ensure that everything is OK.
我会将它们序列化为JSON对象,以确保一切正常。 Like this:
像这样:
<a onclick="SelectBenefit(<%= new JavaScriptSerializer().Serialize(new { id = o.ba_Object_id, code = o.ba_Object_Code }) %>)" href="#">Select</a>
and then the SelectBenefit function might look like this: 然后SelectBenefit函数可能如下所示:
function SelectBenefit(benefit) {
alert(benefit.id);
alert(benefit.code);
}
I'm guessing o.ba_Object_Code
is not a number? 我猜
o.ba_Object_Code
不是一个数字? Try putting quotes around it: 试着在它周围加上引号:
<a onclick="SelectBenefit(<%=o.ba_Object_id %>,'<%=o.ba_Object_Code %>')" href="#">Select</a>
You could also write this function like this: 你也可以像这样编写这个函数:
<a href="javascript:SelectBenefit(<%=o.ba_Object_id %>,'<%=o.ba_Object_Code %>');">Select</a>
Or use Jquery (best approach, imo): 或者使用Jquery(最好的方法,imo):
$('#yourlinkid').click(function(){
SelectBenefit(<%=o.ba_Object_id %>,'<%=o.ba_Object_Code %>');
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.