我正在尝试实现一个搜索框。以下是代码(HTML和JS)
HTML
<form>
<input type="text" ng-model="searchVar" class="searchbox">
<input type="button" value="Search" ng-click="Search()" class="button">
</form>
JS
var Search = function() {
/* code to implement the table content loading part */
//The following is what I have to filter out the table contents based on input in the text field
if (($scope.searchVar) && (tableContent[i].indexOf($scope.searchVar) !== -1)) {
ItemsToDisplay.push(tableContent[i])
}
//Call function to load table
}
发生的情况是,如果我在输入文本字段中输入一些字符串,搜索算法工作正常,并且仅显示相关项目。但是,如果我清除搜索框的内容并单击“搜索”按钮,则表中不会显示任何内容。也就是说,当清除文本字段并单击搜索按钮时,就好像 ItemsToDisplay 为空并且 if 条件失败。
有人能解释一下为什么会出现这种情况吗?我该如何解决这个问题?
最佳答案
在你的indexOf($searchVar)之前你应该检查searchVar是!=''。否则之后将不会显示任何项目。一个建议,javascript 有一个非常棒的 console.log 功能,当涉及到 if 分支时,它会给你很大帮助
关于javascript - 空文本输入框的值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37803833/