如何通过JS更改<select>中当前选定的选项

时间:2018-02-01 23:52:44

标签: javascript html

我尝试了几种方法,但只有我得到的 - 一次两个选定的项目。我也在SOF上发现了一个&#34;解决方案&#34; - document.getElementById(&#34; selection-type&#34;)。selectedIndex = 2; ,但它不起作用。 我知道这是一个非常简单的问题,但我真的不知道我错过了什么。 &#13; &#13; var select = document.body.querySelector(&#39; select&#39;);&#13; &#13; for(var i = 0; i&lt; select.options.length; i ++){&#13; if(select.options [i] .selected){&#13; }&#13; }&#13; &#13; select.options [1] .selected = false;&#13; &#13; var newOption = new Option(&#39; Classic&#39;,&#39; Classic&#39;);&#13; select.append(newOption);&#13; &#13; select.options [2] .selected = true;&#13; &LT;选择&GT;&#13; &lt; option value =&#34; Rock&#34;&gt; Storm&lt; / option&gt;&#13; &lt; option value =&#34; Blues&#34;选择&GT;雨&LT; /选项&GT;&#13; &LT; /选择&GT;&#13; &#13; &#13;

2 个答案:

答案 0 :(得分:3)

您会混淆属性和属性。更改属性是影响内存中对象的动态操作,它不会修改最初解析该元素的静态HTML。如果您想更改属性,则需要使用setAttribute()removeAttribute()

var select = document.body.querySelector('select');

// Get the element with the "selected" attribute
console.log("Element with 'selected' HTML attribute: " + 
   select.querySelector("[selected]").getAttribute("value"));

// Get the element with the "selected" value:
console.log("Element with value property of 'selected': " + select.value);

// Change the value property of the select
console.log("Changing value of select element to 'Rock'");
select.value = "Rock";

// Get the element with the "selected" attribute
console.log("Element with 'selected' HTML attribute: " + 
   select.querySelector("[selected]").getAttribute("value"));  // Still Blues!

// Get the element with the "selected" value:
console.log("But, value of the select element is now: " + select.value);  // Now Rock!

// Change which element has the selected attribute
console.log("Switching elment that has the 'selected' attribute...");
select.querySelector("[selected]").removeAttribute("selected");
select.querySelector("[value=Rock]").setAttribute("selected", "selected");

console.log("Element with 'selected' HTML attribute: " + 
  select.querySelector("[selected]").getAttribute("value"));
  
console.log(select.options[0])
console.log(select.options[1]);
<select>
  <option value="Rock">Storm</option>
  <option value="Blues" selected>Rain</option>
</select>

答案 1 :(得分:1)

要回答标题中的问题,您只需将所需选项设置为已选择。要获得当前<select>值,应该查看select.value

let select = document.body.querySelector('select'),
  newOption = new Option('Classic', 'Classic');

select.append(newOption);

select.options[2].selected = true;

console.log(select.value);
// Classic
<select>
  <option value="Rock">Storm</option>
  <option value="Blues" selected>Rain</option>
</select>

另请注意selected 属性仅标记默认选择,并且与selected的{​​{1}}属性不同。该属性只是告诉浏览器是否应该在最初绘制DOM元素时将选项呈现为选中状态。

处理<option>时,您需要检查<select multiple>的{​​{1}}属性并将其映射到数组。例如:

.selectedOptions
<select>

.selectedOptions是一个对象,其中包含选定的选项let select = document.body.querySelector('select'), newOption = new Option('Classic', 'Classic'); select.append(newOption); select.options[2].selected = true; let values = [].slice.call(select.selectedOptions).map(a => a.value); console.log(values) // ["Blues","Classic"]和原生方法,例如<select multiple> <option value="Rock">Storm</option> <option value="Blues" selected>Rain</option> </select>length。不包含备注选项组。