0

这里有点糊涂。

我有一些由用户填写的表单字段,我基本上需要选择具有特定值的字段并将颜色更改为红色,作为验证辅助。

这就是我的想法:

$('input[value="www.mydomain.com"]').css('color','red');
// or with data from script
$('input[value="'+url+'"]').css('color','red');

但这没有用,所以我做了一些阅读,发现因为我使用的是 jQuery 1.9+,所以这个方法不像这个版本之前那样工作。因此,在阅读了一些 SO 问题后,我发现了这一点:

$("input").filter(function () {
    return this.value === "www.mydomain.com";
});

但不确定这是如何工作的以及如何使用它。这是 jQuery 1.9+ 的正确方法吗?如何通过更改 CSS 等方式使其以与以前相同的方式工作?

4

1 回答 1

2

You're right in using the filter function, this will return an array of elements matching the criteria (in this case, those whose value is www.mydomain.com), so simply chain a .css call and you're good!

$("input").filter(function () {
    return this.value === "www.mydomain.com";
}).css('color','red');
于 2013-09-16T17:35:52.603 回答