javascript 未捕获的引用错误:未定义 False
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20664000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught reference error:False is not defined
提问by R B
I have the following call to javascript:
我对 javascript 有以下调用:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this._BtAdd.Attributes.Add("onclick", "GXDualListBox_MoveDualList(" + sourceListId + ", " + destListId + ", " + selectedValuesId + ", false, true, "+ this._SortByDescription +");");
this._BtRemove.Attributes.Add("onclick", "GXDualListBox_MoveDualList(" + destListId + ", " + sourceListId + ", " + selectedValuesId + ", false, false, " + this._SortByDescription + ");");
if (!this._PostBackOnAll)
{
this._BtAddAll.Attributes.Add("onclick", "GXDualListBox_MoveDualList(" + sourceListId + ", " + destListId + ", " + selectedValuesId + ", true, true, " + this._SortByDescription + " );");
this._BtRemoveAll.Attributes.Add("onclick", "GXDualListBox_MoveDualList(" + destListId + ", " + sourceListId + ", " + selectedValuesId + ", true, false, " + this._SortByDescription + " );");
}
// Check if user can double-click on listbox item to move it
if (this._AllowDblClick)
{
this._LstSource.Attributes.Add("ondblclick", "GXDualListBox_MoveDualList(" + sourceListId + ", " + destListId + ", " + selectedValuesId + ", false, true, " + this._SortByDescription + " );");
this._LstDestination.Attributes.Add("ondblclick", "GXDualListBox_MoveDualList(" + destListId + ", " + sourceListId + ", " + selectedValuesId + ", false, false, " + this._SortByDescription + " );");
}
}
this._SortByDescription is bool which would be false in this case. The javascript is as follows:
this._SortByDescription 是 bool,在这种情况下为 false。javascript如下:
function GXDualListBox_MoveDualList(srcList, destList, selectedValues, moveAll, isAdd,sortByDescription)
{
if ((srcList.selectedIndex == -1) && (moveAll == false)) {
return;
}
newDestList = new Array(destList.options.length);
for (var len = 0; len < destList.options.length; len++) {
if (destList.options[len] != null) {
newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
}
}
for (var i = 0; i < srcList.options.length; i++) {
if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
len++;
}
}
if (sortByDescription) {
newDestList.sort(GXDualListManager_CompareOptionValues);
newDestList.sort(GXDualListManager_CompareOptionText);
}
for (var j = 0; j < newDestList.length; j++) {
if (newDestList[j] != null) {
destList.options[j] = newDestList[j];
}
}
}
if (isAdd)
buildSelectedList(destList, selectedValues);
else
buildSelectedList(srcList, selectedValues);
}
When I hard code this._SortByDescription as 'false' in the javascript call, it works. But replacing 'false' with this._SortByDescription results in error. Also I observed while debugging that the javascript receives the value of this._SortByDescription as 'False' instead of 'false'. Not sure if this matters. I am working on javascript for the first time. Please help.
当我在 javascript 调用中将 this._SortByDescription 硬编码为“false”时,它可以工作。但是用 this._SortByDescription 替换 'false' 会导致错误。此外,我在调试时观察到 javascript 将 this._SortByDescription 的值接收为 'False' 而不是 'false'。不确定这是否重要。我第一次使用 javascript。请帮忙。
回答by DontVoteMeDown
Try converting to lower case:
尝试转换为小写:
this._SortByDescription.ToLower();
If the property of type bool
:
如果 type 的属性bool
:
this._SortByDescription.ToString().ToLower();
回答by Chris Brickner
You could also define "True" and "False" in the beginning of your function/JS. It may seem tedious, but it worked for me.
您还可以在函数/JS 的开头定义“True”和“False”。这可能看起来很乏味,但它对我有用。
function xxx {
var True = true;
var False = false;
// code...
}
回答by HankCa
As discussed, it is False
in the land of c#
, but false
in javascript
. You just have to rejig your code so that it is returning the LC version.
正如所讨论的,它False
在 的土地上c#
,但是false
在javascript
。您只需要重新调整代码,使其返回 LC 版本。
this._SortByDescription ? "true" : "false"
I faced something similar. I had scriptlets in my ASP pages and this failed for the same reason:
我遇到过类似的事情。我的 ASP 页面中有 scriptlet,但由于同样的原因而失败:
isSubSpeciesAspect = <%=Model.aspect.ToLower() === "subspecies"%>;
In the generated JS this came out as:
在生成的 JS 中,结果如下:
isSubSpeciesAspect = True;
I just had to move things around to fix this problem:
我只需要移动一些东西来解决这个问题:
isSubSpeciesAspect = ("<%=Model.aspect.ToLower()%>" === "subspecies");