1

“基本上,我在 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*/
                      }
                      }
                );
                      }
                });
4

1 回答 1

0

您的代码正在搜索数字列中的字符串

也就是说,当您拨打电话时,getText("text_input1")您将获得用户输入为字符串的邮政编码,例如"98101". 但是数据库中的邮政编码存储为数字,例如98101(没有引号!)。由于readRecords()不会自动转换值,因此找不到任何结果。

有两种快速的方法可以解决这个问题。一种方法是parseInt()在搜索之前将用户输入从字符串转换为数字:

readRecords("FreeClothing",{ZipCodes: parseInt(getText("text_input1"))},

另一种是将数据库中的邮政编码转换为字符串。您可以使用隐藏在列顶部齿轮图标后面的“转换为字符串”选项快速执行此操作:

截图

我做了一个示例项目,展示了这种类型的搜索。

于 2019-02-24T05:24:06.147 回答