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

时间:2022-07-05 20:31:57

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中按值选择?

4 个解决方案

#1


33  

$('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/

#2


19  

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

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

When a value is changed by calling .val(), changing the native JS .value or 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' });

#3


2  

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

#4


0  

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

你需要使用引号来包围值

#1


33  

$('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/

#2


19  

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

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

When a value is changed by calling .val(), changing the native JS .value or 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' });

#3


2  

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

#4


0  

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

你需要使用引号来包围值