如何使用 jQuery 1.4.3 及更高版本按值选择输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6439859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:52:25  来源:igfitidea点击:

How to select an input by value with jQuery 1.4.3 and higher

jquery

提问by H1D

With jQuery 1.4.2 and many previous version it is possible to select inputs like this:

使用 jQuery 1.4.2 和许多以前的版本,可以像这样选择输入:

$('input[value=test]')

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?

但是对于 1.4.3 及更高版本,此选择器不起作用 =( 有没有办法在较新版本的 jQuery 中按值进行选择?

采纳答案by H1D

So, the solution is to add this to js:

因此,解决方案是将其添加到 js 中:

$('input').bind('keyup change',function() {
    $(this).attr('value',this.value)
});

Demo - http://jsfiddle.net/VwVhD/28/

演示 - http://jsfiddle.net/VwVhD/28/

UPDATE (21.02.2013)

更新 (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();
        }
    }

回答by Alex

$('input[value="test"]')Should work fine...

$('input[value="test"]')应该可以正常工作...

EDIT

编辑

You could try using if statements, for example:

您可以尝试使用 if 语句,例如:

$("input").keyup(function(e) { 
    if (this.value == 'foo'){
        $(this).css('backgroundColor','red');
    }
    else if (this.value == 'bar'){
        $(this).css('backgroundColor','green');
    }
    else $(this).css('backgroundColor','white');
});

http://jsfiddle.net/ajthomascouk/HrXVS/

http://jsfiddle.net/ajthomascouk/HrXVS/

回答by filip

As far as I can see, in newer jQuery versions, you can select INPUT elements by their "value" attribute, but not by the current .valueproperty.

据我所知,在较新的 jQuery 版本中,您可以通过“”属性来选择 INPUT 元素,但不能通过当前的.value属性来选择。

When a value is changed by calling .val(), changing the native JS .valueor also by direct user input, the HTML attribute is not changed. In former versions, selectors referred to the current value, while now they correctly refer to the HTML attribute.

当通过调用.val()、更改原生 JS .value或直接用户输入更改值时,HTML 属性不会更改。在以前的版本中,选择器引用当前值,而现在它们正确引用 HTML 属性。

You can select however the current value with this code:

但是,您可以使用以下代码选择当前值:

$('input').filter(function() { return this.value == 'test' });

回答by Alex

According to the documentation it should work just fine

根据文档,它应该可以正常工作

http://api.jquery.com/attribute-equals-selector/

http://api.jquery.com/attribute-equals-selector/

you need to use quotes to surround the value

您需要使用引号将值括起来

回答by Michael

By this code you can select/make checked the input (type radio) by value, which is the same like "for" param in his label.

通过此代码,您可以按值选择/检查输入(类型单选),这与标签中的“for”参数相同。

jQuery('label').click(function() {
           labelID = jQuery(this).attr('for'); //find item and save "for" value
           jQuery('input[type=radio][value='+labelID+']').trigger('click') ; // find input with value and click
    });