简体   繁体   English

如何设置选择的默认选项?

[英]How to set default option selected?

In my code I'm using two dropdownlistbox. 在我的代码中,我使用了两个dropdownlistbox。 From the first DDL I have multiple array of values. 从第一个DDL开始,我有多个值数组。 If I select any one of the value and then click the button, the selected value is removed from the first DDL and added into the second DLL. 如果我选择其中一个值,然后单击按钮,则所选值将从第一个DDL中删除,并添加到第二个DLL中。 All these processes are running properly. 所有这些进程均正常运行。

But, how do I set the default selected option into the second DDL? 但是,如何将默认的选定选项设置为第二个DDL? This is my script: 这是我的脚本:

function SelectMoveRows(SS1,SS2)
{
    var SelID='';
    var SelText='';
    var count=0;
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);           
            SS2.options[SS2.length]=newRow;            
            SS1.options[i]=null;
            //count++;//by raghu
        }
    }
}

Seeing as the arguments for new Option are 作为new Option的参数是

new Option(text, value, defaultSelected, selected);

Did you try 你试过了吗

var newRow = new Option(SelText,SelID, true, true);

Another option would be to set the selects value to the same as SelID after the option is inserted. 另一个选项是在插入选项后将选择值设置为与SelID相同。

SS2.value = SelID;

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

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