Javascript的奇怪问题。会发生什么:
问题:
功能go(选择){
var nextChar = choice;
nextChar++;
$.ajax({
type: "GET",
url: "../xml/movielist.xml",
dataType: "xml",
success: function (xml)
{
var link = '<a href="';
var linkMid = '">';
var linkEnd = "</a>";
var output = "";
$(xml).find('movie').each(function () //For each <Movie> tag, retrieve the following:
{
var title = $(this).find('title').text() //Retreive Movie's Title
if ((title.substring(0, 5) == "The " + String.fromCharCode(choice)) || (title.substring(0, 1) == String.fromCharCode(choice)) || (title.substring(0, 3) == "A " + String.fromCharCode(choice)))
{
/*Deleted lines here to save space and they also don't apply to the problem at hand*/
output = output + "<tr>" + rowOut + "</td>";
}
if (title.substring(0, 1) == String.fromCharCode(nextChar))
{
document.getElementById("results").innerHTML = output;
return false;
}
})
}
}) };
示例XML代码。这是前两部以字母S开头的电影。
<movie>
<title>Salt</title>
<link>films/salt.aspx</link>
<runtime>1h 34m</runtime>
</movie>
<movie>
<title>See No Evil</title>
<link>films/seenoevil.aspx</link>
<runtime>1h 24m</runtime>
</movie>
让我重申一下,这个脚本效果很好,并将结果输出到屏幕上除了S,P和Z之外的每一个字母(虽然我知道为什么Z不起作用,我还没有编写代码)。我不知道为什么S和P表现不同。通过一些调试,我推断当用户选择S或P时,甚至从未输入If语句,这意味着即使变量“choice”等于80或83,也不会满足If语句中的条件(P或整个练习中的S分别)。
我确定这是愚蠢的事,但我似乎无法真正地绕过它。
任何建议都将不胜感激。提前谢谢!
答案 0 :(得分:0)
尝试使用choice == title.substring(0, 1).charCodeAt()
答案 1 :(得分:0)
固定。问题是在If语句和错误设计中混合了不合逻辑的条件。当循环遇到以下一个字母开头的电影时,程序将返回false。因此,如果用户选择字母“S”,脚本将遍历XML文件并查找以字母S开头的所有电影。在这种情况下,当循环播放以电影开头的电影时,循环将退出。字母“T”。
if (title.substring(0, 1) == String.fromCharCode(nextChar))
{
document.getElementById("results").innerHTML = output;
return false;
}
这段代码对此负责。问题是当用户选择“S”时,循环将一直运行,直到它击中以字母“T”开头的电影。有许多电影以“The”开头,例如“实习”。这会导致循环过早退出。
为了解决这个问题,我将休息条件更改为:
if (choice == 83) //If the user chose the letter "S", use this condition to break the loop instead. Special Case!
{
if ((title.substring(0, 5) == "The T"))
{
document.getElementById("results").innerHTML = output;
return false;
}
}
else if ((String.fromCharCode(nextChar) == title.substring(0, 1)) && (title.substring(0, 5) != "The " + String.fromCharCode(choice))) //Otherwise exit normally
{
document.getElementById("results").innerHTML = output;
return false;
}
这确保了循环在包括S在内的所有情况下都能正常退出。如果用户选择字母S,它将退出if和仅当下一部电影以字母T或“T”开头但不是以“S”。
新的和功能完备的代码看起来与此类似(我编辑了不必要的线条以保持整洁和节省空间):
$(xml).find('movie').each(function () //For each <Movie> tag, retrieve the following:
{
var title = $(this).find('title').text() //Retreive Movie's Title
if ((choice == title.substring(0, 1).charCodeAt()) || (title.substring(0, 3) == "A " + String.fromCharCode(choice))) //If the first letter of the movie starts with the user's chosen letter OR If the movie starts with A_ and the user's chosen letter
{
//Do stuff here
}
if ((title.substring(0, 5) == "The " + String.fromCharCode(choice))) //If the movie starts with The_ and the user's chosen letter
{
//Otherwise, do stuff here
}
if (choice == 83) //If the user chose the letter "S", use this condition to break the loop instead. Special Case!
{
if ((title.substring(0, 5) == "The T"))
{
document.getElementById("results").innerHTML = output;
return false;
}
}
else if ((String.fromCharCode(nextChar) == title.substring(0, 1)) && (title.substring(0, 5) != "The " + String.fromCharCode(choice))) //Otherwise exit normally
{
document.getElementById("results").innerHTML = output;
return false;
}