如何更新单词列表中搜索框中包含用户键入内容的单词/字母数
How to update the number of words/letters containing what user type in a search box in a list of words?
当用户搜索任何字母/单词时,我一直试图让小部件框上方的行("总共10个单词")动态更改,但未能成功。该行应更改为例如"包含‘ab’的2个单词"。
我使用了这行代码,但没有起作用:
var len= matches.length;
$("#WordCounter".text(len+"contain"+$("#typeahead").val()));
有人知道怎么做吗?
var substringMatcher = function(strs, q, cb) {
return (function(q, cb, name) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push(name(str));
}
});
cb(matches);
}(q, cb, function(res){return res}));
};
var states = [
"a",
"able",
"about",
"account",
"acid",
"across",
"act",
"addition",
"adjustment",
"advertisement",
];
$.each(states, function (key, value) {
$("#selection").append($("<option/>").val(key).text(value));
});
$("#typeahead").on("input", function(e) {
substringMatcher(states, e.target.value, function(results) {
$("#selection").empty();
$.map(results, function(value, index) {
$("#selection").append($("<option/>", {
"class":"results" + index,
"html":value
}))
})
})
});
$("#clearButton").click(results);
html {
background-color: #C0C0C0;
}
div{
width:400px;
margin:0px auto;
position: absolute;
left: 20px;
}
select{
width:400px;
margin:40px auto;
position: absolute;
left: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body><div>Find: <input type="text" id="typeahead" />
<button id= "clearButton">Clear</button></div><br />
<div> <p id= "WordCounter">10 words in total</p></div>
<select size="7" id="selection">
</select>
您的代码包含错误的闭包。必须是:
var len= matches.length;
$("#WordCounter").text(len + " contain " + $("#typeahead").val());
所以最终的html将是
var substringMatcher = function(strs, q, cb) {
return (function(q, cb, name) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push(name(str));
}
});
cb(matches);
}(q, cb, function(res){return res}));
};
var states = [
"a",
"able",
"about",
"account",
"acid",
"across",
"act",
"addition",
"adjustment",
"advertisement",
];
$.each(states, function (key, value) {
$("#selection").append($("<option/>").val(key).text(value));
});
$("#typeahead").on("input", function(e) {
substringMatcher(states, e.target.value, function(results) {
var len= results.length;
$("#WordCounter").text(len + " contain " + $("#typeahead").val());
$("#selection").empty();
$.map(results, function(value, index) {
$("#selection").append($("<option/>", {
"class":"results" + index,
"html":value
}))
})
})
});
$("#clearButton").click(results);
html {
background-color: #C0C0C0;
}
div{
width:400px;
margin:0px auto;
position: absolute;
left: 20px;
}
select{
width:400px;
margin:40px auto;
position: absolute;
left: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body><div>Find: <input type="text" id="typeahead" />
<button id= "clearButton">Clear</button></div><br />
<div> <p id= "WordCounter">10 words in total</p></div>
<select size="7" id="selection">
</select>
相关文章:
- 跳过用户输入的文本字符串中的特殊字符,并在 Javascript 中的每个单词后添加连字符
- Hangman游戏将用户选择的字母与单词中的字母进行比较以进行猜测
- 确保用户在继续之前完成给定的单词
- Javascript Hangman游戏如何保存用户输入的单词
- 如何更新单词列表中搜索框中包含用户键入内容的单词/字母数
- 创建一个输入,该输入在用户键入后立即不断更新单词列表
- 如果突出显示某个单词,并且用户单击了连接单词,请突出显示这两个单词
- Firebase应该在我的用户id结构中包含单词“;simplelogin”;
- 当用户键入一个单词并按下回车键时,用Javascript打开弹出菜单
- 当用户选择文本并按delete/backspace键时,不会触发AngularJS的单词计数指令
- 刽子手协助.当用户用尽所有尝试或猜到单词中的所有字母时,我需要停止游戏
- 自动更正用户输入的单词
- 如何使用JavaScript正则表达式在单词边界之间找到用户提供的字符串
- 通过几个用户的输入进行搜索,并揭示其中最常见的单词
- 防止用户在文本区输入某些单词
- 当用户输入时替换单词(JS)
- 如何在用户在文本列表中键入单词时突出显示JSON字典中的单词,并在表单提交时用它们的描述替换它们
- 如何确定用户是否开始使用JS中的新单词
- 如何防止用户在文本框中写一些特定的单词-JQuery
- 过滤掉用户在 javascript 中键入文本时与顺序不匹配的单词