html默认值设置及修改,如何设置HTML

Set selected="selected" for the option you want to be the default.

3

In case you want to have a default text as a sort of placeholder/hint but not considered a valid value (something like "complete here", "select your nation" ecc.) you can do something like this:

Choose here

One

Two

Three

Four

Five

Complete example:

1

2

3

4

5

I came across this question, but the accepted and highly upvoted answer didn't work for me. It turns out that if you are using React, then setting selected doesn't work.

Instead you have to set a value in the tag directly as shown below:

Apple

Banana

Cranberry

You can do it like this:

1

2

3

4

5

Provide "selected" keyword inside the option tag, which you want to appear by default in your drop down list.

Or you can also provide attribute to the option tag i.e.

3

if you want to use the values from a Form and keep it dynamic try this with php

$selected = $_POST['select'];

?>

>1

>2

I prefer this:

Choose here

One

Two

Three

Four

Five

'Choose here' disappears after an option has been selected.

An improvement for nobita's answer. Also you can improve the visual view of the drop down list, by hiding the element 'Choose here'.

Choose here

One

Two

Three

Four

Five

Another example; using JavaScript to set a selected option.

(You could use this example to for loop an array of values into a drop down component)

// Get the select element

var select = document.getElementById("yourDropDownElementId");

// Create a new option element

var el = document.createElement("option");

// Add our value to the option

el.textContent = "Example Value";

el.value = "Example Value";

// Set the option to selected

el.selected = true;

// Add the new option element to the select element

select.appendChild(el);

The selected attribute is a boolean attribute.

When present, it specifies that an option should be pre-selected when the page loads.

The pre-selected option will be displayed first in the drop-down list.

Volvo

Saab

VW

Audi

If you are in react you can use defaultValue as attribute instead of value in the select tag.

If you are using select with angular 1, then you need to use ng-init, otherwise, second option will not be selected since, ng-model overrides the defaul selected value

Name

Stars

Language

I used this php function to generate the options, and insert it into my HTML

# code to output a set of options for a numeric drop down list

# parameters: (start, end, step, format, default)

function numericoptions($start, $end, $step, $formatstring, $default)

{

$retstring = "";

for($i = $start; $i <= $end; $i = $i + $step)

{

$retstring = $retstring . '

$retstring = $retstring . 'value="' . sprintf($formatstring,$i) . '"';

if($default == $i)

{

$retstring = $retstring . ' selected="selected"';

}

$retstring = $retstring . '>' . sprintf($formatstring,$i) . '

';

}

return $retstring;

}

?>

And then in my webpage code I use it as below;

If $endmin is created from a _POST variable every time the page is loaded (and this code is inside a form which posts) then the previously selected value is selected by default.

This code sets the default value for the HTML select element with PHP.

$default = 3;

$nr = 1;

while($nr < 10){

if($nr == $default){

echo "". $nr ."";

}

else{

echo "". $nr ."";

}

$nr++;

}

?>

value attribute of tag is missing, so it doesn't show as u desired selected. By default first option show on dropdown page load, if value attribute is set on tag.... I got solved my problem this way

You can use:

Some Value

instead of,

Some Value

both are equally correct.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值