Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态选择框页面加载_Javascript - Fatal编程技术网

Javascript 动态选择框页面加载

Javascript 动态选择框页面加载,javascript,Javascript,我有一个动态链式选择框,试图在页面加载时显示其值。在我的链接选择框中,它将默认为页面加载时选择框中的第一个选项,有人可以提供帮助吗 我偶然发现了这条线索,但我似乎无法将他们用这个答案做的事情翻译成我的CF语言。 下面是我正在使用的JS脚本 function dynamicSelect(id1, id2) { // Feature test to see if there is enough W3C DOM support if (document.getElementB

我有一个动态链式选择框,试图在页面加载时显示其值。在我的链接选择框中,它将默认为页面加载时选择框中的第一个选项,有人可以提供帮助吗

我偶然发现了这条线索,但我似乎无法将他们用这个答案做的事情翻译成我的CF语言。

下面是我正在使用的JS脚本

function dynamicSelect(id1, id2) {  
    // Feature test to see if there is enough W3C DOM support  
    if (document.getElementById && document.getElementsByTagName) {  
        // Obtain references to both select boxes  
        var sel1 = document.getElementById(id1);  
        var sel2 = document.getElementById(id2); 

        // Clone the dynamic select box  
        var clone = sel2.cloneNode(true);  
        // Obtain references to all cloned options  
        var clonedOptions = clone.getElementsByTagName("option"); 

        // Onload init: call a generic function to display the related options in the dynamic select box  
        refreshDynamicSelectOptions(sel1, sel2, clonedOptions);  
        // Onchange of the main select box: call a generic function to display the related options in the dynamic select box  
        sel1.onchange = function() {  
            refreshDynamicSelectOptions(sel1, sel2, clonedOptions); 
        } 
    }  
}  

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {  
    // Delete all options of the dynamic select box  
    while (sel2.options.length) {  
        sel2.remove(0);  
    }  

    // Create regular expression objects for "select" and the value of the selected option of the main select box as class names  
    var pattern1 = /( |^)(select)( |$)/;  
    var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");  
    // Iterate through all cloned options  
    for (var i = 0; i < clonedOptions.length; i++) {  
        // If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box  
        if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {  
            // Clone the option from the hidden option pool and append it to the dynamic select box  
            sel2.appendChild(clonedOptions[i].cloneNode(true));  
        }                  
    }  
}

非常感谢您的帮助

所以您遇到的问题就是将第二个下拉菜单默认设置为第一个选项


sel2.selectedIndex=0

在调用refreshDynamicSelectOptions之前,将原始选定索引保存在dynamicSelect函数中,如下所示:

var nOriginalSelection = sel2.selectedIndex;
将此消息传递给refreshDynamicSelectOptions函数:

refreshDynamicSelectOptions(sel1, sel2, clonedOptions, nOriginalSelection); 
添加到函数声明中:

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions, sel2Index) {
最后,在该函数中,在附加所有匹配选项后添加此行:

sel2.selectedIndex = sel2Index;

你需要什么帮助?我诚挚的道歉,但我不知道你的问题是什么?如果我对我的问题不清楚,我很抱歉。当我使用动态下拉列表提交页面时,第一个选择框将保留其值,类似于selected=selected的工作方式。但是,第二个下拉列表将不会保留其值,它将默认为其列表中的第一项。这有助于澄清吗?我希望找到一种方法,使第二个选择列表,链接列表,在屏幕上保留其价值。大卫不会看到你的评论-通知某人使用@就像我在这里使用的一样。萨莫,谢谢你的回复。是,我需要sel2的selectedIndex显示在重新加载页面上。Sry我是这样一个n00b:哦,你的意思是它必须设置为与第一个下拉列表更改之前设置的索引相同的索引。这是一个奇怪而傲慢的要求。但如果这就是你想要的,我想影子巫师会有你的答案。非常感谢你的帮助