检查数组是否包含keyUp上的输入文本

时间:2017-03-06 16:33:53

标签: javascript regex

我搜索了这个,我发现所有答案都包含A | 2017-03-01 08:30 A | 2017-03-01 12:30 B | 2017-03-01 08:30 B | 2017-03-01 12:30 ,由于某种原因,我的选项不起作用:

我有一个数组:

indexof

现在当我使用indexOf时它只在我输入整个单词时起作用,我需要它:

var array = ["Sunny","Snowy","Rainy"];
var input = document.getElementById('input').value;

以下是代码:

Input: S
Result: Sunny, Snowy
Input: Su
Result: Sunny

没有jquery的简单javascript。

1 个答案:

答案 0 :(得分:1)

您的Sub PasteArray(TargetArray() As Variant, RangeName As String, Optional blNotThisWorkbook As Boolean = False) 'The purpose of this function is to be able to transfer data from an array in VBA back into a 'named range in excel Dim strWorksheet As String Dim strAddress As String Dim cntRows As Long Dim cntCols As Long Dim WBook As Workbook Dim rngCols As Long Dim rngRows As Long 'Check if the array is empty, if it is then exit the sub If SafeUbound(TargetArray) = 0 Then Exit Sub If blNotThisWorkbook Then Set WBook = ActiveWorkbook Else Set WBook = ThisWorkbook End If 'Clear out the paste range to start WBook.Names(RangeName).RefersToRange.ClearContents 'Find the name of the target worksheet strWorksheet = WBook.Names(RangeName).RefersToRange.Worksheet.Name 'Find the size of the range we are pasting to rngCols = WBook.Names(RangeName).RefersToRange.Columns.Count rngRows = WBook.Names(RangeName).RefersToRange.Rows.Count 'Find the number of rows and columns cntRows = UBound(TargetArray) cntCols = UBound(TargetArray, 2) 'Check to make sure the array will fit in the range we are pasting to. 'trip an error here if desired If cntRows > rngRows Or cntCols > rngCols Then Set WBook = Nothing Exit Sub End If 'Get the exact size of the range, based on the amount of data in it SplitAd = Split(WBook.Names(RangeName).RefersToRange.Address, ":") strAddress = SplitAd(0) & ":" & Range(SplitAd(0)).Offset(cntRows - 1, cntCols - 1).Address 'Drop down the array into the range WBook.Worksheets(strWorksheet).Range(strAddress).Value = TargetArray Set WBook = Nothing End Sub 操作数向后倾斜。 indexOf()也是一个DOM元素,您要问DOM元素是否包含数组元素,而不是询问DOM元素的是否在数组中。以下是如何修改它的方法:



input

var array = ["Sunny", "Snowy", "Rainy"];
var input = document.getElementById('input');

input.addEventListener("input", function() {
  for (var i = 0; i < array.length; i++) {
    if (array[i].indexOf(this.value) > -1) {
      console.log("Object with index number " + i + " contains " + this.value);
    }
  }
});
&#13;
&#13;
&#13;

编辑 - 根据OP的使用内联键盘事件处理程序的请求,如果输入值为空则不返回任何结果 - 注意:使用keyup事件不是建议,因为它不捕获鼠标切割&amp;粘贴,并在修改键上不必要地触发(shift,ctrl,alt等)。 <input type="text" id="input" />事件在这里更有用。

&#13;
&#13;
input
&#13;
var array = ["Sunny", "Snowy", "Rainy"];
var input = document.getElementById('input');

function handleKeyup(elem) {
  // return nothing if empty value in input
  if (elem.value !== "") {
    for (var i = 0; i < array.length; i++) {
      if (array[i].indexOf(elem.value) > -1) {
        console.log("Object with index number " + i + " contains " + elem.value);
      }
    }
  }
}
&#13;
&#13;
&#13;