如何使用jQuery 1.4.3及更高版本按值选择输入 [英] How to select an input by value with jQuery 1.4.3 and higher
本文介绍了如何使用jQuery 1.4.3及更高版本按值选择输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery 1.4.2和许多以前的版本,可以选择这样的输入:
With jQuery 1.4.2 and many previous version it is possible to select inputs like this:
$('input[value=test]')
但是在1.4.3和更高版本中,此选择器不起作用=( 在新版本的jQuery中,有什么方法可以按值选择吗?
But with 1.4.3 and higher this selector doesn't work =( Is there any way to select by value in newer versions of jQuery?
推荐答案
因此,解决方案是将其添加到js:
So, the solution is to add this to js:
$('input').bind('keyup change',function() {
$(this).attr('value',this.value)
});
演示- http://jsfiddle.net/VwVhD/28/
更新(21.02.2013)
几年后,我想到了这个:
After years I came up with this actually:
jQuery('input,textarea').live('keyup change', function(e) {
var ignore_codes = [16,9,33,34,36,35,45,38,40,37,39];//arrows and others
if (e.keyCode && jQuery.inArray(e.keyCode, ignore_codes) < 0)//ignore this keyUps to let this keys work as expected
{
var cp = getCP(this);
jQuery(this).attr('value', this.value);
setCP(this,cp);
}
});
function setCP(ctrl, pos){
if(ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
} else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
这篇关于如何使用jQuery 1.4.3及更高版本按值选择输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文