标签上的value属性未选择默认选项

时间:2022-11-27 07:35:01

Perhaps i'm misunderstanding here, but given the following html:

也许我在这里误解了,但是给出了以下html:

<select value="2">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

I would expect "Something else" to be the default selected option. However, it does not seem to be. Why is this, and what should I be doing differently?

我希望“其他东西”成为默认选择的选项。但是,似乎并非如此。为什么会这样,我应该做些什么呢?

5 个解决方案

#1


9  

The only way to have a default option is to have selected in the option tag.

拥有默认选项的唯一方法是在选项标签中选择。

<SELECT>
<OPTION>Something
<OPTION SELECTED>Something Else

#2


17  

You use selected attribute on an option element to specify default option.

您可以在选项元素上使用selected属性来指定默认选项。

<select>
    <option value="1">Something</option>
    <option value="2" selected="selected">Something else</option> // this is default
</select>

select elements do not have a value attribute.

select元素没有value属性。

#3


1  

The <select> element does not have a value attribute so that is ignored. So, you have a single selection <select> and none of its <option> elements have the selected attribute, that means that the first <option> is taken as the default selection.

,并且其

#4


1  

React JS

Some coding implementations such as ReactJS allow you to use a value attribute with the <select> tag so this would be perfectly valid code:

某些编码实现(如ReactJS)允许您使用带有

<select value="2">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

HTML with Attribute Minimization:

具有属性最小化的HTML:

Of course, typically you would want to specify the value in state, so that it is updateable.

当然,通常您需要在state中指定值,以便它是可更新的。

However, if you are using purely HTML you must use the selected attribute in your <option> tag as follows:

但是,如果您使用的是纯HTML,则必须使用

<select>
    <option value="1">Something</option>
    <option value="2" selected>Something else</option>
</select>

HTML with Full Attribute Specification:

具有完整属性规范的HTML:

The above uses attribute minimization, but you can also specify the full form if you want:

以上使用属性最小化,但您也可以根据需要指定完整表单:

<select>
    <option value="1">Something</option>
    <option value="2" selected="selected">Something else</option>
</select>

#5


0  

I know this post is quite old but in case anyone else is struggling with this you can implement the functionality you are looking for using jquery.

我知道这篇文章很老了但是如果其他人都在努力解决这个问题,你可以使用jquery实现你正在寻找的功能。

The full code using php would be something like this

使用php的完整代码就是这样的

PHP

if ($_SERVER['REQUEST_METHOD']== "POST") {
$thing = $_POST['things'];
} else {
$thing ="";
}

HTML

<select name='things' value="<?php echo $thing; ?>">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

JQUERY

$(function() {
    $("select[value]").each(function() {
        $(this).val(this.getAttribute("value"));
    });
}); //end document ready

This will allow the select options chosen by the user to remain selected after the page has re-loaded via post instead of returning to the default values.

这将允许用户选择的选项在页面通过post重新加载后保持选中状态,而不是返回默认值。

#1


9  

The only way to have a default option is to have selected in the option tag.

拥有默认选项的唯一方法是在选项标签中选择。

<SELECT>
<OPTION>Something
<OPTION SELECTED>Something Else

#2


17  

You use selected attribute on an option element to specify default option.

您可以在选项元素上使用selected属性来指定默认选项。

<select>
    <option value="1">Something</option>
    <option value="2" selected="selected">Something else</option> // this is default
</select>

select elements do not have a value attribute.

select元素没有value属性。

#3


1  

The <select> element does not have a value attribute so that is ignored. So, you have a single selection <select> and none of its <option> elements have the selected attribute, that means that the first <option> is taken as the default selection.

,并且其

#4


1  

React JS

Some coding implementations such as ReactJS allow you to use a value attribute with the <select> tag so this would be perfectly valid code:

某些编码实现(如ReactJS)允许您使用带有

<select value="2">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

HTML with Attribute Minimization:

具有属性最小化的HTML:

Of course, typically you would want to specify the value in state, so that it is updateable.

当然,通常您需要在state中指定值,以便它是可更新的。

However, if you are using purely HTML you must use the selected attribute in your <option> tag as follows:

但是,如果您使用的是纯HTML,则必须使用

<select>
    <option value="1">Something</option>
    <option value="2" selected>Something else</option>
</select>

HTML with Full Attribute Specification:

具有完整属性规范的HTML:

The above uses attribute minimization, but you can also specify the full form if you want:

以上使用属性最小化,但您也可以根据需要指定完整表单:

<select>
    <option value="1">Something</option>
    <option value="2" selected="selected">Something else</option>
</select>

#5


0  

I know this post is quite old but in case anyone else is struggling with this you can implement the functionality you are looking for using jquery.

我知道这篇文章很老了但是如果其他人都在努力解决这个问题,你可以使用jquery实现你正在寻找的功能。

The full code using php would be something like this

使用php的完整代码就是这样的

PHP

if ($_SERVER['REQUEST_METHOD']== "POST") {
$thing = $_POST['things'];
} else {
$thing ="";
}

HTML

<select name='things' value="<?php echo $thing; ?>">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

JQUERY

$(function() {
    $("select[value]").each(function() {
        $(this).val(this.getAttribute("value"));
    });
}); //end document ready

This will allow the select options chosen by the user to remain selected after the page has re-loaded via post instead of returning to the default values.

这将允许用户选择的选项在页面通过post重新加载后保持选中状态,而不是返回默认值。