迭代选择集合时,val()失败(jquery)(val() fails when iterating through a collection of selects (jquery))

我有一个包含多个元素的表单,我想迭代它们,并将所选元素(在每个个体中)的值与字符串进行比较。 好像我想要使用val(),但......

$("#FormSection select[class='input-small']").each(function (a, b) { if (b.val()!="test") alert('hello'); });

在上面,它说“val”是未定义的。 但是b.id确实等于其中一个选择元素。

如果我直接使用jquery直接获取相同的select元素,val()按预期工作。 所以,我将循环内的b与直接抓取的选择进行了比较。 差异:当直接抓取时,jQuery返回一个单元素数组。 关于val()的文档提到它需要一个数组,所以也许这是预期的。

有趣的是,有一个属性“b.value”正是我所期望的,但我不知道它是否与jquery方法通常的跨浏览器兼容。

我很难找到这个,因为我的关键字返回的结果有s允许多项选择。

任何想法都会非常感激!

谢谢..-Ben

I've got a form with multiple elements, and I want to iterate through them, and compare the value of the selected element (in each individual ) with a string. Seems like I would want to use val(), but...

$("#FormSection select[class='input-small']").each(function (a, b) { if (b.val()!="test") alert('hello'); });

In the above, it says that "val" is undefined. But b.id is indeed equal to one of the select elements.

If I alternatively directly use jquery to grab the same select element directly, val() works as expected. So, I compared b from within the loop to the directly grabbed select. And the difference: When directly grabbed, jQuery returned a single-element array. The documentation about val() mentioned that it expects an array, so maybe this is expected.

Interestingly, there's a property "b.value" which has exactly what I expect, but I don't know if it's cross-browser compatible the way jquery methods often are.

I had a really tough time searching for this because my keywords returned results regarding s that allow multiple selection.

Any thoughts would be really appreciated!

Thanks.. -Ben

最满意答案

你需要将它包装在$()以使用val() 。 val()不是DOM元素上的有效方法,但正如您所提到的,您不需要这样做,而只需使用DOM元素的.value属性。

$("#FormSection select[class='input-small']").each(function (idx, b) { if (b.value !="test") // b represents DOM Element select, $(b) represents jquery object representing DOM element. //if(this.value != test) or just use this. this context here is the element of current iteration. alert('hello'); });

You need to wrap it in $() to use val(). val() is not a valid method on DOM element but as you mentioned you dont need to do that, instead you could just use .value property of the DOM element.

$("#FormSection select[class='input-small']").each(function (idx, b) { if (b.value !="test") // b represents DOM Element select, $(b) represents jquery object representing DOM element. //if(this.value != test) or just use this. this context here is the element of current iteration. alert('hello'); });迭代选择集合时,val()失败(jquery)(val() fails when iterating through a collection of selects (jquery))

我有一个包含多个元素的表单,我想迭代它们,并将所选元素(在每个个体中)的值与字符串进行比较。 好像我想要使用val(),但......

$("#FormSection select[class='input-small']").each(function (a, b) { if (b.val()!="test") alert('hello'); });

在上面,它说“val”是未定义的。 但是b.id确实等于其中一个选择元素。

如果我直接使用jquery直接获取相同的select元素,val()按预期工作。 所以,我将循环内的b与直接抓取的选择进行了比较。 差异:当直接抓取时,jQuery返回一个单元素数组。 关于val()的文档提到它需要一个数组,所以也许这是预期的。

有趣的是,有一个属性“b.value”正是我所期望的,但我不知道它是否与jquery方法通常的跨浏览器兼容。

我很难找到这个,因为我的关键字返回的结果有s允许多项选择。

任何想法都会非常感激!

谢谢..-Ben

I've got a form with multiple elements, and I want to iterate through them, and compare the value of the selected element (in each individual ) with a string. Seems like I would want to use val(), but...

$("#FormSection select[class='input-small']").each(function (a, b) { if (b.val()!="test") alert('hello'); });

In the above, it says that "val" is undefined. But b.id is indeed equal to one of the select elements.

If I alternatively directly use jquery to grab the same select element directly, val() works as expected. So, I compared b from within the loop to the directly grabbed select. And the difference: When directly grabbed, jQuery returned a single-element array. The documentation about val() mentioned that it expects an array, so maybe this is expected.

Interestingly, there's a property "b.value" which has exactly what I expect, but I don't know if it's cross-browser compatible the way jquery methods often are.

I had a really tough time searching for this because my keywords returned results regarding s that allow multiple selection.

Any thoughts would be really appreciated!

Thanks.. -Ben

最满意答案

你需要将它包装在$()以使用val() 。 val()不是DOM元素上的有效方法,但正如您所提到的,您不需要这样做,而只需使用DOM元素的.value属性。

$("#FormSection select[class='input-small']").each(function (idx, b) { if (b.value !="test") // b represents DOM Element select, $(b) represents jquery object representing DOM element. //if(this.value != test) or just use this. this context here is the element of current iteration. alert('hello'); });

You need to wrap it in $() to use val(). val() is not a valid method on DOM element but as you mentioned you dont need to do that, instead you could just use .value property of the DOM element.

$("#FormSection select[class='input-small']").each(function (idx, b) { if (b.value !="test") // b represents DOM Element select, $(b) represents jquery object representing DOM element. //if(this.value != test) or just use this. this context here is the element of current iteration. alert('hello'); });