Javascript 我很难使按钮正常工作
我有一个文本框,它可以匹配隐藏容器id中的单词,并在按下enter键后将其淡入,除了我添加了一个按钮来执行相同的操作外,其他一切都很好…我似乎无法使按钮正常工作 HTML 这里有一个解决方案 此部分已更改:Javascript 我很难使按钮正常工作,javascript,jquery,button,Javascript,Jquery,Button,我有一个文本框,它可以匹配隐藏容器id中的单词,并在按下enter键后将其淡入,除了我添加了一个按钮来执行相同的操作外,其他一切都很好…我似乎无法使按钮正常工作 HTML 这里有一个解决方案 此部分已更改: $('#search').focus().keyup(function (e) { var search = $(this); var _search = search.val(); if (e.which === 13){ show_page(_
$('#search').focus().keyup(function (e) {
var search = $(this);
var _search = search.val();
if (e.which === 13){
show_page(_search);
}
});
$('.btn').click(function(){
show_page($('#search').val());
});
function show_page(_search) {
substringMatcher(template.content, _search, function (d) {
$("#" + d[0].content)
.delay(500)
.fadeIn(500)
.siblings()
.fadeOut(500);
search.val("")
})
}
我制作了一个func来显示页面,返回和按钮都使用它。你说你不能用按钮来显示页面,但我看不到有人试图让它工作。您尝试过什么?为什么要使用anon函数
子字符串匹配器
返回anon函数?您从未将函数绑定到按钮,因此不会有任何结果happen@forgivenson我通常会删除所有失败的尝试。@alessandroniciforo我这样做了,但显然是错误的,我通常会在尝试修复某些东西时删除失败的尝试。
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) {
$("#search").val("");
if (substrRegex.test(str) || q.slice(0, 1) === str.slice(0, 1)) {
// 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 (n) {
return {
"content": n
}
}));
};
var _matches = $.map($("#content div"), function (v, k) {
return [v.id]
});
var template = {
"content": _matches
};
var search = $('#search').val().toLowerCase();
$("#content div:gt(0)").hide(0);
$('#search').focus().keyup(function (e) {
var search = $(this);
var _search = search.val();
if (e.which === 13){
substringMatcher(template.content, _search, function (d) {
$("#" + d[0].content)
.delay(500)
.fadeIn(500)
.siblings()
.fadeOut(500);
search.val("")
})
}
});
$('#search').focus().keyup(function (e) {
var search = $(this);
var _search = search.val();
if (e.which === 13){
show_page(_search);
}
});
$('.btn').click(function(){
show_page($('#search').val());
});
function show_page(_search) {
substringMatcher(template.content, _search, function (d) {
$("#" + d[0].content)
.delay(500)
.fadeIn(500)
.siblings()
.fadeOut(500);
search.val("")
})
}