提交后如何在钻取组合框中保留选定的值

时间:2016-02-26 07:42:43

标签: javascript php html-form

我有一个关于使用php和javascript钻取组合框的问题。

当我选择父数据时,子数据变为可选;它是用javascript控制的。

我尝试在发布后保留这两个值,但以下代码仅保留父数据。提交后如何在钻取组合框中保留这两个值?

HTML&安培; PHP

<select id="SEL1" name="SEL1"> 
<option value="">---</option> 
<?php 
foreach ($parent_list as $parent => $children_list) { 
echo '<option value="'.$parent.'"'; 
    if ($parent == $_GET['SEL1']){ 
     echo 'selected'; 
    } 
echo '>'.$parent.'</option>'; 
} 
?> 
</select> 

<select id="SEL2" name="SEL2"> 
<option value="">---</option> 
<?php 
foreach ($parent_list as $parent => $children_list) { 
echo '<optgroup label="'.$parent.'">'; 
foreach ($children_list as $children => $grandson_list) { 
    $view_children = str_replace($parent,"",$children); 
    echo '<option value="'.$children.'"'; 
        if ($children == $_GET['SEL2']){ 
         echo 'selected'; 
        } 
    echo '>'.$view_children.'</option>'; 
} 
echo '</optgroup>'; 
} 
?> 
</select>

的javascript

function ConnectedSelect(selIdList){ 
    for(var i=0;selIdList[i];i++) { 
        var CS = new Object(); 
        var obj = document.getElementById(selIdList[i]); 
        if(i){ 
            CS.node=document.createElement('select'); 
            var GR = obj.getElementsByTagName('optgroup'); 
            while(GR[0]) { 
                CS.node.appendChild(GR[0].cloneNode(true)); 
                obj.removeChild(GR[0]); 
            } 
            obj.disabled = true; 
        } 
        if(selIdList[i+1]) { 
            CS.nextSelect = document.getElementById(selIdList[i+1]); 
            obj.onchange = function(){ConnectedSelectEnabledSelect(this)}; 
        } else { 
            CS.nextSelect = false; 
        } 
        obj.ConnectedSelect = CS; 
    } 
}

function ConnectedSelectEnabledSelect(oSel){ 
    var oVal = oSel.options[oSel.selectedIndex].value; 
    if(oVal) { 
        while(oSel.ConnectedSelect.nextSelect.options[1])oSel.ConnectedSelect.nextSelect.remove(1); 
        var eF = false; 
        for(var OG=oSel.ConnectedSelect.nextSelect.ConnectedSelect.node.firstChild;OG;OG=OG.nextSibling) { 
            if(OG.label == oVal) { 
                eF = true; 
                for(var OP=OG.firstChild;OP;OP=OP.nextSibling) 
                    oSel.ConnectedSelect.nextSelect.appendChild(OP.cloneNode(true)); 
                break; 
            } 
        } 
        oSel.ConnectedSelect.nextSelect.disabled = !eF; 
    } else { 
        oSel.ConnectedSelect.nextSelect.selectedIndex = 0; 
        oSel.ConnectedSelect.nextSelect.disabled = true; 
    } 
    if(oSel.ConnectedSelect.nextSelect.onchange)oSel.ConnectedSelect.nextSelect.onchange();
}

ConnectedSelect(['SEL1','SEL2']);

//--> 
</script>

0 个答案:

没有答案