将选项添加到选择列表中 [英] Adding options into a select list

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

问题描述

你好!



有没有办法可以根据在不同选择列表中选择的选项将选项添加到不同的选择下拉列表中?



Hi there!

Is there a way I can add options into a different select drop down depending on what option is selected in a DIFFERENT select list?

function functionName(){
if (document.getElementById('selectID1').value == "abc") {
	//selectList2 adds 2 options
	document.getElementById('selectID2').
}
else{
    //selectList2 keeps the current options
    document.getElementbyId('selectID2').
}










<select name="selectName1" id="selectID1" onchange="functionName()">
          <option value="123">option 1</option>
          <option value="abc">option 2</option>

        </select>
        <select name="selectName2" id="selectID2">
            <option value="def1">default option 1</option>
            <option value="def2">default option 2</option>
        </select>





我希望能够有两个不同的选项代替已经存在的两个默认选项。



可见性属性是一个好方法吗?还是有更好的方法来做我不知道的事情?



I want to be able to have two different options in the place of the two default ones that are already there.

Is the visibility attribute a good method? or is there a better way to do this that I''m not aware of?

推荐答案

你好,



这是一个小片段,展示了如何使用JavaScript为下拉列表添加选项。
Hello,

Here is a small snippet showing how to add options to a dropdown using JavaScript.
var cb = document.getElementById('selectID1');
if (cb.options[cb.selectedIndex].value == "abc") {
    var cb2 = document.getElementById('selectID2');
    clearSelect(cb2);
    addOption(cb2, "Text1", "value1");
}
function clearSelect(ctrl) {
    for (var i = ctrl.options.length - 1; i >= 0; i--) {
        ctrl.remove(i);
    }
}
function adoption(ctrl, txt, val) {
    var option = document.createElement("option");
    option.text = txt;
    option.value = val;
    ctrl.add(option, null);
}



问候,


Regards,


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

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