【问题标题】:How to change the value of the text or select a dropdown value by using document.getElementById("id") [duplicate]如何使用 document.getElementById("id") 更改文本的值或选择下拉值
【发布时间】:2020-02-19 08:14:38
【问题描述】:

如何选择带有我们使用 document.getElementById("id") 的值的选项:

document.getElementById("dropdownlist").selectedValue="apple";

或在 javascript 代码中借助 this 更改控件的文本。

我正在像这样填充下拉列表:

 <select id="ddlC" class="form-control" ng-model="mdlC" runat="server" style="width: 250px" ng-options="c.CId as c.Na for c in Cat">
                <option value="">Select Category</option>
            </select>

【问题讨论】:

标签: javascript angularjs


【解决方案1】:

selectedValue 更改为 value

document.getElementById("dropdownlist").value = "apple";

【讨论】:

  • 它没有被选中
  • 你有选项中的值属性吗? &lt;option value='apple'&gt;Apple&lt;/option&gt; 你的 DOM 中是否有 ID dropdownlist 唯一?
  • 是的,我有这个
  • 等等,我想我没有那个抱歉,让我检查一下,非常感谢
  • 告诉我。不客气。
【解决方案2】:

试试这个

    var element = document.getElementById("dropdownlist");
    element.value = "apple";

【讨论】:

    【解决方案3】:

    看看this one too

    通过document.getElementById("dropdownlist").value = "option3";,您可以设置要选择的特定选项

    var dropDownOptions = ["option1", "option2", "option3", "others"]
     var options = "";
     
    for (option in dropDownOptions) {
        options += "<option>" + dropDownOptions[option] + "</option>";
    }
    document.getElementById("dropdownlist").innerHTML = options;
    
    // select option by value
    document.getElementById("dropdownlist").value = "option3";
    &lt;select name="category" id="dropdownlist"/&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      相关资源
      最近更新 更多