我认为在下面的<select>元素上添加一个“value”属性集会导致默认选择包含我提供的“value”的<option>:

<select name=“hall”id=“hall”value=“3”><option>1</option><option>2</option><option>3</option><选项>4</选项><选项>5</选项></选择>

然而,这并没有像我预期的那样奏效。我如何设置默认选择哪个<option>元素?


当前回答

我遇到了这个问题,但这个被接受的、高度支持的答案对我来说不起作用。事实证明,如果你使用的是React,那么设置selected就不起作用了。

相反,您必须直接在<select>标记中设置一个值,如下所示:

<select value="B">
  <option value="A">Apple</option>
  <option value="B">Banana</option>
  <option value="C">Cranberry</option>
</select>

在React页面上阅读更多关于原因的信息。

其他回答

我更喜欢这样:

<select>
   <option selected hidden>Choose here</option>
   <option value="1">One</option>
   <option value="2">Two</option>
   <option value="3">Three</option>
   <option value="4">Four</option>
   <option value="5">Five</option>
</select>

选择选项后,“选择此处”消失。

缺少标记的value属性,因此它不会显示为所需的选定项。默认情况下,如果在标签上设置了value属性,则在下拉页面加载时显示第一个选项。。。。我这样解决了我的问题

我只需将第一个选择选项值设置为默认值,然后使用HTML5的新“隐藏”功能将该值隐藏在下拉列表中。这样地:

   <select name="" id="">
     <option hidden value="default">Select An Option</option>
     <option value="1">One</option>
     <option value="2">Two</option>
     <option value="3">Three</option>
     <option value="4">Four</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>

这是选择默认选项的简单方法。

可用于在HTML页面上进行多次选择。

方法:

查找每个选择读取所选内容的id和值选择选项

注:

每个选择都必须具有ID以避免冲突

$(文档).ready(函数){//为页面中的每个选择循环$('select').each(函数(索引,id){//获取值var theValue=$(this).attr('value');//获取IDvar theID=$(this).attr('id');//选择Make选项$('select#'+theID+'选项[value='+theValue+']').attr('selected',true);});});<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js“></script><select id=“sport”name=“sport”class=“autoselect”value=“golf”><option value=“basket”>篮球</option>网球</option>高尔夫</option><option value=“保龄球”>保龄球</option></选择><小时><select id=“tools”name=“tools”class=“autoselect”value=“saw”><option value=“hammer”>锤子</option><option value=“drill”>钻孔</option><option value=“螺丝刀”>螺丝刀</option><option value=“saw”>锯</option><option value=“扳手”>扳手</option></选择>