在select>选项上的jQuery $ .value,其中没有value属性

时间:2021-06-14 20:34:04

Given this HTML code:

鉴于此HTML代码:

<select id="menu">
    <option>hi</option>
    <option>bye</option>
</select>

This jQuery code:

这个jQuery代码:

$("#menu option").first().attr("value")

returns hi, even though there is no value attribute on the first option in the menu.

返回hi,即使菜单中的第一个选项没有value属性。

If the <option> has a value attribute, the value attribute is returned, as expected.

如果

How can I have the return value of the above code be undefined / something else I can identify? Or could I check if the value attribute is set, and only run the above code if it isn't?

如何将上述代码的返回值设置为未定义/我可以识别的其他内容?或者我可以检查是否设置了value属性,如果不是,则只运行上面的代码?

5 个解决方案

#1


18  

Use the :not([attr]) selector:

使用:not([attr])选择器:

$("#menu option:not([value])");

http://jsfiddle.net/userdude/NQm54/

I also verified that after changing, the [value] still reports as unset, so you continue to receive the same two elements without the value attribute.

我还验证了更改后,[value]仍然报告为未设置,因此您继续接收不带value属性的相同两个元素。

$('#menu').change(function(){
  console.log($(this).val());
  console.log($("#menu option:not([value])"));
});

http://jsfiddle.net/userdude/NQm54/2/

#2


2  

$("#menu option:eq(0)").not("[value]").length == 1

#3


1  

The value defaults to the text value if the value attribute is not set. Just set the attribute value t o some value so you can identify it e.g. an empty string as shown below

如果未设置value属性,则默认值为文本值。只需将属性值设置为某个值,以便您可以识别它,例如一个空字符串,如下所示

<select id="menu">
    <option value="">hi</option>
    <option>bye</option>
</select>

Then this $("#menu option").first().attr("value") will return an empty string

然后这个$(“#menu option”)。first()。attr(“value”)将返回一个空字符串

#4


1  

var option = document.getElementById("menu").options[0];
if (option.getAttribute("value") !== null) {
console.log("Has value: " + option.value);
 }
else {
console.log("Doesn't have a value.");
}

#5


0  

Use this $("option:selected", $("#menu")).attr("value");

使用此$(“选项:已选择”,$(“#menu”))。attr(“value”);

If the selected option does not have any value attribute, undefined will be returned which you can capture through !foo

如果所选选项没有任何值属性,将返回undefined,您可以通过!foo捕获

#1


18  

Use the :not([attr]) selector:

使用:not([attr])选择器:

$("#menu option:not([value])");

http://jsfiddle.net/userdude/NQm54/

I also verified that after changing, the [value] still reports as unset, so you continue to receive the same two elements without the value attribute.

我还验证了更改后,[value]仍然报告为未设置,因此您继续接收不带value属性的相同两个元素。

$('#menu').change(function(){
  console.log($(this).val());
  console.log($("#menu option:not([value])"));
});

http://jsfiddle.net/userdude/NQm54/2/

#2


2  

$("#menu option:eq(0)").not("[value]").length == 1

#3


1  

The value defaults to the text value if the value attribute is not set. Just set the attribute value t o some value so you can identify it e.g. an empty string as shown below

如果未设置value属性,则默认值为文本值。只需将属性值设置为某个值,以便您可以识别它,例如一个空字符串,如下所示

<select id="menu">
    <option value="">hi</option>
    <option>bye</option>
</select>

Then this $("#menu option").first().attr("value") will return an empty string

然后这个$(“#menu option”)。first()。attr(“value”)将返回一个空字符串

#4


1  

var option = document.getElementById("menu").options[0];
if (option.getAttribute("value") !== null) {
console.log("Has value: " + option.value);
 }
else {
console.log("Doesn't have a value.");
}

#5


0  

Use this $("option:selected", $("#menu")).attr("value");

使用此$(“选项:已选择”,$(“#menu”))。attr(“value”);

If the selected option does not have any value attribute, undefined will be returned which you can capture through !foo

如果所选选项没有任何值属性,将返回undefined,您可以通过!foo捕获