两个选择列表之间的JavaScript分配不起作用

时间:2018-11-09 17:41:38

标签: javascript html asp.net-mvc

我有一个MVC项目,其中有两个selectlists:一个显示给用户,一个隐藏的包含我的视图数据,其中显示的数据来自该视图。

我的触发另一个selectlist的onchange的Javascript函数如下:

function select()
    {
        document.getElementById("SelectedServer").innerHTML = document.getElementById("ServerSelect").value;
        var services = document.getElementById("ServiceSelect");
        services.options = serviceoptions.options;
        console.log(services.options === serviceoptions.options)
        var servers = document.getElementById("ServerSelect").selectedOptions;
        for (i = 0; i < services.length; i++)
        {
            for (j = 0; j < servers.length; j++)
            {
                    if (services.options[i].value == servers.item(j).value) { }
                    else (services.options[i] = null)
            }
        }
    }

此功能使用选定的服务器更新文本字段,为我的服务selectlist分配来自隐藏数据selectlist的选项,称为“ serviceoptions”,然后对其进行排序,以便仅显示服务如果它们与所选服务器匹配(使用value)。

由于某种原因,当我执行选项分配(services.options = serviceoptions.options;)时,它运行时没有错误,但显然不能正确分配选项。

我测试的控制台输出返回false,而chrome调试器显示长度尚未更新。

我想将serviceoptions的options变量分配给services的options变量。为什么我不能这样做?

我要用隐藏字段解决的问题是,以前,如果服务不匹配,我的服务会正确消失,但是如果用户选择了另一台服务器,我将无法恢复服务。

我不能使用ajax进行另一个API调用来再次检索服务器/服务。

0 个答案:

没有答案