jQuery live 问题:在选择列表过滤功能中,live 不适用于 keyup 事件
我有一个 jQuery 脚本,它正在过滤 html 选择列表。 当我使用 keyup 事件时它工作正常,但我最近尝试使用 live("keyup"
,但它不起作用。
这是脚本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<meta name="Distribution" content="Global" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Select list filter</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//simple keyup
;(function($) {
$.fn.selectFilter = function() {
var sel = $(this);
$(this).after("<input style='display: block;' type='text' />");
$(this).css("display", "block");
if($(this).attr("size")<2)
{
$(this).attr("size", "3");
}
$(this).next("input[type='text']").keyup(function(){
sel.children('option').hide();
var a = new Array();
var txt = $(this).val().toLowerCase();
sel.children('option').each(function(i, selected){
var pattern=new RegExp(txt);
var value = $(this).text().toLowerCase();
if(pattern.test(value))
{
$(selected).show();
}
});
});
};
})(jQuery);
//live keyup
;(function($) {
$.fn.liveSelectFilter = function() {
var sel = $(this);
$(this).after("<input style='display: block;' type='text' />");
$(this).css("display", "block");
if($(this).attr("size")<2)
{
$(this).attr("size", "3");
}
$(this).next("input[type='text']").live("keyup", function(){
sel.children('option').hide();
var a = new Array();
var txt = $(this).val().toLowerCase();
sel.children('option').each(function(i, selected){
var pattern=new RegExp(txt);
var value = $(this).text().toLowerCase();
if(pattern.test(value))
{
$(selected).show();
}
});
});
};
})(jQuery);
jQuery(document).ready(function(){
jQuery("#select-list").selectFilter();
jQuery("#live-select-list").liveSelectFilter();
});
</script>
</head>
<body>
<form action="" method="post">
Keyup:
<select id="select-list" size="5" name='test' style="width:200px">
<option value='1'>John Smith</option>
<option value='2'>John Doe</option>
<option value='3'>Jason Bradley</option>
<option value='4'>Bob Smith</option>
<option value='5'>Jane Doe</option>
<option value='6'>David White</option>
<option value='7'>Neal Johnson</option>
<option value='8'>Richard Bradman</option>
</select>
<br /><br />Live keyup:
<select id="live-select-list" size="5" name='test' style="width:200px">
<option value='1'>John Smith</option>
<option value='2'>John Doe</option>
<option value='3'>Jason Bradley</option>
<option value='4'>Bob Smith</option>
<option value='5'>Jane Doe</option>
<option value='6'>David White</option>
<option value='7'>Neal Johnson</option>
<option value='8'>Richard Bradman</option>
</select>
</form>
</body>
</html>
如何解决这个问题?
I have a jQuery script, which is filtering an html select list.
It works fine when I use the keyup event, but I recently tried to use instead the live("keyup"
, but it doesn't work.
Here is the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<meta name="Distribution" content="Global" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Select list filter</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//simple keyup
;(function($) {
$.fn.selectFilter = function() {
var sel = $(this);
$(this).after("<input style='display: block;' type='text' />");
$(this).css("display", "block");
if($(this).attr("size")<2)
{
$(this).attr("size", "3");
}
$(this).next("input[type='text']").keyup(function(){
sel.children('option').hide();
var a = new Array();
var txt = $(this).val().toLowerCase();
sel.children('option').each(function(i, selected){
var pattern=new RegExp(txt);
var value = $(this).text().toLowerCase();
if(pattern.test(value))
{
$(selected).show();
}
});
});
};
})(jQuery);
//live keyup
;(function($) {
$.fn.liveSelectFilter = function() {
var sel = $(this);
$(this).after("<input style='display: block;' type='text' />");
$(this).css("display", "block");
if($(this).attr("size")<2)
{
$(this).attr("size", "3");
}
$(this).next("input[type='text']").live("keyup", function(){
sel.children('option').hide();
var a = new Array();
var txt = $(this).val().toLowerCase();
sel.children('option').each(function(i, selected){
var pattern=new RegExp(txt);
var value = $(this).text().toLowerCase();
if(pattern.test(value))
{
$(selected).show();
}
});
});
};
})(jQuery);
jQuery(document).ready(function(){
jQuery("#select-list").selectFilter();
jQuery("#live-select-list").liveSelectFilter();
});
</script>
</head>
<body>
<form action="" method="post">
Keyup:
<select id="select-list" size="5" name='test' style="width:200px">
<option value='1'>John Smith</option>
<option value='2'>John Doe</option>
<option value='3'>Jason Bradley</option>
<option value='4'>Bob Smith</option>
<option value='5'>Jane Doe</option>
<option value='6'>David White</option>
<option value='7'>Neal Johnson</option>
<option value='8'>Richard Bradman</option>
</select>
<br /><br />Live keyup:
<select id="live-select-list" size="5" name='test' style="width:200px">
<option value='1'>John Smith</option>
<option value='2'>John Doe</option>
<option value='3'>Jason Bradley</option>
<option value='4'>Bob Smith</option>
<option value='5'>Jane Doe</option>
<option value='6'>David White</option>
<option value='7'>Neal Johnson</option>
<option value='8'>Richard Bradman</option>
</select>
</form>
</body>
</html>
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
.live()
适用于选择器而不是指定的元素。引用自文档
和
因此,您应该更改将实时事件绑定到
this.selector
的方式,将返回使用的原始选择器,并且+ input[type='text']
是 下一个相邻选择器实时地址 http://jsfiddle.net/gaby/u9uhV/
The problem is that
.live()
works with selectors and not specified elements.Quoting from the docs
and
So you should change the way you bind the live event to
this.selector
will return the original selector used and the+ input[type='text']
is the next adjacent selectorlive at http://jsfiddle.net/gaby/u9uhV/