简体   繁体   English

如何设置 HTML 的默认值<select>元素与 JavaScript?

[英]How can I set the default value for an HTML <select> element with JavaScript?

How to set the 'selected' (default) value for an HTML <select> element with JavaScript.如何使用 JavaScript 为 HTML <select>元素设置“selected”(默认)值。 Aka.阿卡。 currently option0 is 'selected', how to run a script to change it to display the value i want?当前选项 0 被“选中”,如何运行脚本来更改它以显示我想要的值?

this for example because that value is previously saved in a database, and i only want it updated if the user actually specifies to do so.例如,这是因为该值以前保存在数据库中,我只希望在用户实际指定时更新它。 But if i don't specify the value (by re-selecting the previous option), saving the 'edit' will overwrite the previous value with the 'default selected' value the <select> is loaded with.但是如果我不指定值(通过重新选择上一个选项),保存“编辑”将用加载<select>的“默认选择”值覆盖前一个值。

<select id="selector" name="selector">
  <option id="option0" value="0" selected=true >default </option>
  <option id="option1" value="1">option 1 </option>
  <option id="option1" value="2">option 2 </option>
  <option id="option1" value="3">option 3 </option>
  <option id="option1" value="4">option 4 </option>
</select>

NOTE : Because i lack reputation to add my answer (below) to the general thread on this topic, here the solution i got to using javascript, to set an option to be the option displayed/selected in the selector.注意因为我缺乏将我的答案(如下)添加到该主题的一般线程的声誉,所以这里的解决方案是我使用 javascript,将选项设置为在选择器中显示/选择的选项。 This can also be a <%= value %> from a database.这也可以是来自数据库的 <%= value %>。

Use value property to set new selected value using JavaScript使用value属性使用 JavaScript 设置新的选定值

 document.getElementById('selector').value="2";
 <select id="selector" name="selector"> <option id="option0" value="0" selected=true >default </option> <option id="option1" value="1">option 1 </option> <option id="option1" value="2">option 2 </option> <option id="option1" value="3">option 3 </option> <option id="option1" value="4">option 4 </option> </select>

<script> 
    let options = document.getElementsByTagName('option');

    for(i=0; i<options.length; i++){
        if (options[i].value === "<%= or_some_string %>"){
            options[i].selected = true;
        }
    } 
</script>

We get all the options (this is an html collection) and iterate over it to check if any option is equal to the "String" or "<%=string_value_from_database%>".我们获取所有选项(这是一个 html 集合)并对其进行迭代以检查是否有任何选项等于“String”或“<%=string_value_from_database%>”。 And set it's .selected to true.并将它的 .selected 设置为 true。

NOTE - MAYBE BETTER - DEFFO SHORTER: with help in the comments:注意 - 可能更好 - DEFFO 更短:在评论中提供帮助:

<script>
  let to_select = document.getElementById('selector');
  to_select.value = "<%= value %>";
</script>

Where to_select.value is either set to the value you want, or to a database value.其中 to_select.value 要么设置为您想要的值,要么设置为数据库值。 Do note, you may or may not need the ".." around the value you set, depending on the setup of the <select> and how you stored your data.请注意,根据<select>的设置以及您存储数据的方式,您可能需要也可能不需要您设置的值周围的“..”。

use this用这个

document.getElementById("selector").selectedIndex = 3;

original 原来的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM