简体   繁体   English

使用javascript在选择下拉菜单中更改选项

[英]Change option in select drop down using javascript

Firstly apologies if this is a dumb question, I'm rather new to javascript. 首先道歉,如果这是一个愚蠢的问题,我对javascript很陌生。

I have a select box which is generated by my organisations CMS. 我有一个由组织CMS生成的选择框。 I can't change the drop down's content or how it is generated. 我无法更改下拉列表的内容或生成方式。 What I would like to do however is when the page is loaded, change the default (or "selected") option based on a particular value. 但是,我想做的是在加载页面时,根据特定值更改默认(或“选定”)选项。 The other problem is the default is already set in the select drop down is created in HTML. 另一个问题是在HTML中创建的select下拉列表中已经设置了默认值。 If I could actually change the way options for the select drop down I could easily create some javascript to set the default selection, unfortunately I can't. 如果我实际上可以更改选择下拉菜单的方式,那么我可以轻松创建一些javascript来设置默认选择,但是不幸的是我不能。 I'm completely lost. 我完全迷路了。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。

Code used: 使用的代码:

I unfortunately cannot change this: 不幸的是,我无法更改此设置:

<select name="q528696:q1" id="q528696_q1"  class="sq-form-field"><option value="0" selected="selected">Address&nbsp;adult&nbsp;language&nbsp;literacy&nbsp;and&nbsp;numeracy&nbsp;|&nbsp;688</option>*More options here*</select>

This is the javascript used. 这是使用的javascript。 It's very simple: 很简单:

<SCRIPT> window.onload=updateSelect(); 
function updateSelect() { 
document.getElementById('q528696:q1').value=25; 
}; 
</SCRIPT>

To change a selected option using JavaScript is very simple. 使用JavaScript更改选定的选项非常简单。 But still, I'll break it down. 但是,我将其分解。 Let's start off with a simple drop down list with three options. 让我们从一个带有三个选项的简单下拉列表开始。 The select tag will have an id of "change". 选择标签的ID为“更改”。

<select id="change">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>

We want it so that when we press a button "Two" will be the selected option instead of "One". 我们想要它,以便当我们按下按钮时,将选择“ Two”而不是“ One”。 We'll start making our button and making a basic function. 我们将开始制作按钮并制作基本功能。

<input type="button" value="Change Selected Option" onclick="changeS()"/>
<script>
function changeS() {

}
</script>

In this function we add the following code 在此函数中,我们添加以下代码

document.getElementById('change').getElementsByTagName('option')[1].selected = true;

Let's take a look at what it's doing. 让我们看看它在做什么。

First, we target the "select" tag with document.getElementById . 首先,我们使用document.getElementById定位“ select”标签。
Then we get all the elements with a tag name of "option" in the "select" element. 然后,在“选择”元素中获得标签名称为“选项”的所有元素。
The "[1]" means that we target the second option in the list. “ [1]”表示我们将列表中的第二个选项作为目标。 The reason we have "1" not "2" is because JavaScript starts counting at "0". 之所以有“ 1”而不是“ 2”,是因为JavaScript从“ 0”开始计数。 If we wanted to target the third option, we would put "[2]". 如果我们要定位第三个选项,则应输入“ [2]”。
After that, we just say that we want that specific option in the select tag to be the selected option. 之后,我们只是说我们希望select标签中的特定选项成为被选中的选项。

I hope it's simple enough and that my explaining was worthwhile :) 我希望它足够简单,并且我的解释值得:)

I'm answering to this question: How do I change the value of a select element's option? 我在回答这个问题:如何更改选择元素的选项的值? Here's code with comments to help out. 这是带有注释的代码以提供帮助。

<!--First we'll make a a select element with an option-->
<select>
<option value="Unchanged Value" id="1"></option>
</select>
<!--Now we'll create a button that'll display the value of this option. Refer to the "displayOptionValue" function down below to find out how this is done-->
<input type="button" onclick="displayOptionValue()" value="Display Option Value"/>
<<input type="button" onclick="changeOptionValue()" value="Change Option Value"/>
<!--Here's where the value will display-->
<p id="demo"/>
<script>
//This will get the option's value and display it in the "p" tag
function displayOptionValue() {
var x = document.getElementById("1").value;
document.getElementById("demo").innerHTML = x;
}
//Now we'll change the value
function changeOptionValue() {
document.getElementById("1").value = "Changed Value";
}
</script>

That's what I understand the question to be. 这就是我理解的问题。 If it's not, tell me in the comments and I'll post another answer. 如果不是,请在评论中告诉我,我将发布另一个答案。 Also, if you run the code in your browser, first press "Display Option Value" then "Change Option Value" and then press "Display Option Value" again to see the changes. 另外,如果您在浏览器中运行代码,请先按“显示选项值”,然后按“更改选项值”,然后再次按“显示选项值”以查看更改。 Once again, if this isn't your question, let me know in the comments. 再说一次,如果这不是您的问题,请在评论中告诉我。

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

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