字符串是未定义
我有我的的.aspx的代码隐藏以下属性:字符串是未定义
protected string CurrentProductView
{
get
{
string viewName = string.Empty;
viewName = Enum.GetName(typeof(ProductView), currentProdView);
return viewName;
}
}
在我的.aspx我有一些JavaScript试图引用这个字符串:
$(document).ready(function()
{
var action = <%=CurrentProductView %>
$("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function()
{
$("#recommendationsView").show();
});
});
但由于某种原因,我得到“项目未定义”。
当我调试这个,我肯定看到一个字符串回来viewName。那么为什么它会抱怨如果一个字符串回来?!?!
更改此:
var action = <%=CurrentProductView %>
这样:
var action = "<%=CurrentProductView %>"
既然你打印出一个字符串值的页面到一个变量,你需要周围的引号,因为该值观看作为页面上JavaScript的字面值。你不需要围绕整数引号,因为在JavaScript中这是合法的:
var my_number = 4;
如果做不到
var my_string = this is a string;
,它需要是这样的:
var my_string = "this is a string";
更新我想我不明白为什么......因为它是jQuery?因为我们已经定义了类型为int的其他变量,如下所示:var productID = ; – PositiveGuy 2010-06-28 15:12:11
由于产品ID是一个int,并且在JavaScript中,整数不需要定义围绕它们的引号。 – kemiller2002 2010-06-28 15:12:39
不是我在想什么,但我对aspx不够熟悉,无法确定。 (例如,字符串值可能是自动引用的,例如。) – 2010-06-28 15:12:47
你能分享与Javascript交互的代码? – 2010-06-28 15:08:55
你不应该在字符串周围有引号吗? 'var action =“”;' – 2010-06-28 15:09:16
不,它引用了服务器端属性。 – PositiveGuy 2010-06-28 15:10:04