“基本上,我在code.org上开发一个应用程序,有一个叫: SubmitButtonFreeClothingScreen的按钮。当点击这个按钮时,它会打开一个名为"Screen2”的屏幕,其中包含一个名为"display_info“的文本框。机器通过一个名为"FreeClothing”的数据库进行搜索,我想让它搜索一个术语并显示该术语的数据。用户在一个名为"text_input1“的文本框中输入该术语。我以为下面的代码可以工作,但机器总是显示else语句。还有没有其他方法可以做到这一点?”
onEvent("SubmitButtonFreeClothingScreen", "click", function(event)
{
{
setScreen("Screen2");
readRecords("FreeClothing",{ZipCodes: getText("text_input1")},
function(records)
{
var names="";
if (records.length>0)
{
for(var i =0; i < records.length; i++)
{
var number= i+1;
names=names+ number + ") " + records[i].Name+"" +"\n\n"+"Address: "+ records[i].Address+"\n\n"+"Phone Number: "+ records[i].PhoneNumber+"\n\n"+"Description: "+ records[i].Description+"\n\n";
}
setText("display_info",""+names);
}
/*Display Furniture Store information located in database*/
else
{
setText ("display_info", "This Zipcode unfortunately has no Furniture Store locations. Please try another one");
/*If no information is found, the string above is printed*/
}
}
);
}
});发布于 2019-02-24 13:24:06
也就是说,当您调用getText("text_input1")时,您将得到用户以字符串形式键入的邮政编码,例如"98101"。但是数据库中的邮政编码是以数字形式存储的,比如98101 (没有引号!)。因为readRecords()不会自动转换值,所以它找不到任何结果。
有两种快速解决此问题的方法。一种方法是在搜索之前使用parseInt()将用户输入从字符串转换为数字:
readRecords("FreeClothing",{ZipCodes: parseInt(getText("text_input1"))},另一种方法是将数据库中的邮政编码转换为字符串。您可以通过隐藏在列顶部齿轮图标后面的“转换为字符串”选项快速完成此操作:

我制作了一个example project,展示了这种搜索的实际效果。
https://stackoverflow.com/questions/54736297
复制相似问题