如何使用javascript将选项添加到HTML表单下拉列表中 [英] how do I add an option to a html form dropdown list with javascript

查看:169
本文介绍了如何使用javascript将选项添加到HTML表单下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当您打开列表时,我希望选项为 test

code>和 hello



我做错了什么?

 < SCRIPT> 
函数runList(){
document.getElementById('list')。value =< option> hello< / option>;
}
< / SCRIPT>

< FORM NAME =myformACTION =METHOD =GET>
您的选项:
< INPUT TYPE =buttonNAME =buttonVALUE =ClickonClick =runList()/>
< SELECT NAME =listID =list>
< OPTION>测试< / OPTION>
< / SELECT>


解决方案

您需要将选项对象添加到dom 。我已经链接到您的示例工作的小提琴: http://jsfiddle.net/DS8TG/



将runList更改为以下内容:

  function runList(){
var select = document.getElementById('list');
select.options [select.options.length] = new Option('Hello','Hello');
}


I want this script to add an option to the list.

When you open the list I want the options to be test and hello

What am I doing wrong?

<SCRIPT>
function runList(){
    document.getElementById('list').value = "<option>hello</option>";
}
</SCRIPT>

<FORM NAME="myform" ACTION="" METHOD="GET">
Your Options:
<INPUT TYPE="button" NAME="button" VALUE="Click" onClick="runList()"/>
<SELECT NAME="list" ID="list">
<OPTION>test</OPTION>
</SELECT>

解决方案

You need to actually add the option object to the dom. I have linked to a fiddle with your example working: http://jsfiddle.net/DS8TG/

Change runList to the following:

function runList(){
  var select = document.getElementById('list');
  select.options[select.options.length] = new Option('Hello', 'Hello');
}​

这篇关于如何使用javascript将选项添加到HTML表单下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆