我认为在下面的<select>元素上添加一个“value”属性集会导致默认选择包含我提供的“value”的<option>:
<select name=“hall”id=“hall”value=“3”><option>1</option><option>2</option><option>3</option><选项>4</选项><选项>5</选项></选择>
然而,这并没有像我预期的那样奏效。我如何设置默认选择哪个<option>元素?
我认为在下面的<select>元素上添加一个“value”属性集会导致默认选择包含我提供的“value”的<option>:
<select name=“hall”id=“hall”value=“3”><option>1</option><option>2</option><option>3</option><选项>4</选项><选项>5</选项></选择>
然而,这并没有像我预期的那样奏效。我如何设置默认选择哪个<option>元素?
当前回答
如果您希望将默认文本作为占位符/提示,但不认为它是有效值(例如“此处完成”、“选择您的国家”ecc),您可以执行以下操作:
<选择><option value=“”selected disabled hidden>选择此处</option><option value=“1”>一个</option><option value=“2”>两个</option><option value=“3”>三个</option><option value=“4”>四个</option><option value=“5”>五个</option></选择>
其他回答
我遇到了一些问题,因为我需要根据数据库中的值动态选择选项。下面的剧本对我来说很有魅力:
<?php
//pick the value of database
$selected_sexo = $query['s_sexo'];
?>
<select name="s_sexo" id="s_sexo" required>
<option <?php if($selected_sexo == 'M'){echo("selected");}?> value="M">M</option>
<option <?php if($selected_sexo == 'F'){echo("selected");}?> value="F">F</option>
</select>
这些答案对我来说都不起作用,原因是我的数据库中有一个数组,其中包含以前的选择,我希望用户在更改之前看到高亮显示上次所做的选择。我也在使用不喜欢所选标签的React。
VM71 react_devtools_backend.js:3973 Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>
因此,我决定直接操纵docref.options。在React内部,您必须使用useRef钩子来执行此操作,因此您将看到实际Ref所在的.current属性。通过在html行中添加带有标记select的ref=,可以实现这一点。
这里是useEffect代码,对于那些不使用React的人来说,只需将代码从钩子中取出,这段代码在React每次呈现组件时都会执行。
useEffect(() => {
if(optionRef?.current!==undefined) { // only do something when optionRef has been assigned a doc reference by React
[...optionRef.current.options]?.filter(option => option.value === optionTypes?.filter(type => type===option.value)[0])?.map(option=> option.selected=true)
// because optionTypes is an array of strings, due to using multiple selection, in my case only one match is possible, thus just convert the first and only string of the resulting array to string with the [0]
// this one liner is all that you need if not using react, I only tested it in React
}
}) // I am making it trigger every time it renders, couldn't make it work when tracking optionRef changes or optionRef.current changes, it wouldn't trigger despite the change of optionRef. To make it work I'd probable have to change a state variable every time that optionRef changes, so I decided to just execute this code for every render
React返回中的HTML部分:
<select
name="optionType"
id="idNumber"
ref={optionRef}
multiple={true}
size={3}
> {/* determines the number of elements that will fit into the selection window*/}
<option value="" disabled hidden>Choose Option</option>
<option value="Option1">Option1</option>
</select>
要使用PHP和JavaScript设置默认值:
State: <select id="State">
<option value="" selected disabled hidden></option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option>
.
.
<option value="West Bengal">West Bengal</option>
</select>
<?php
if(isset($_GET['State'])){
echo <<<heredoc
<script>
document.getElementById("State").querySelector('option[value="{$_GET['State']}"]').selected = true;
</script>
heredoc;
}
?>
另一个例子;使用JavaScript设置所选选项。
(您可以使用此示例将值数组循环到下拉组件中)
<select id=“yourDropDownElementId”><select/>
// 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);
为要作为默认选项的选项设置selected=“selected”。
<option selected="selected">
3
</option>