填充以previous下拉值第三个菜单第三个、菜单、previous

2023-09-10 15:52:48 作者:回忆在慢慢死去

我有三个下拉菜单中的前两个做工精细,第三个是给我同样的头痛。 出于某种原因,一旦第二下拉值改变它失去了第一菜单的值。这里是code:

I have three drop down menus the first two work fine, the third one is giving me same head aches. For some reason once the second drop down values changes it loses the value of the first menu. Here is the code:

   <script language="javascript" type="text/javascript">
       function getXMLHTTP() { //function to return the xml http object
            var xmlhttp=false;  
            try{xmlhttp=new XMLHttpRequest();
            }
            catch(e){try{   xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e){
                    try{
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch(e1){
                        xmlhttp=false;
                    }}}
                return xmlhttp;
        }
        function getColor(CategoryId) {     

            var strURL="getColor.php?Category="+CategoryId;
            var req = getXMLHTTP();

            if (req) {

                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {                        
                            document.getElementById('qcolor').innerHTML=req.responseText;                       
                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }               
                }           
                req.open("GET", strURL, true);
                req.send(null);
            }       
        }

在此之前这一切似乎工作,其余的是一些错误,但不知道是什么:

Until here it all seems to work the rest the is something wrong but not sure what:

function getBrand(CategoryId,ColorId) {     

    var strURL="getBrand.php?Category="+CategoryId+"&Color="+ColorId;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('qbrand').innerHTML=req.responseText;                       
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}

在HTML code:

The HTML Code:

 </head>
    <body>
    <div id="Quick_find_2">
             <div id="Quick_find_container">
               <form action="search2.php" method="get">
                 <div id="qcategory_1">Product</div>
                 <div id="qcategory">
                   <select name="Category" class="dropmenu" id="Category" onChange="getColor(this.value)">
                     <option value="">Any</option>
                     <option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
                     <option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
                   </select>
                 </div>

                 <div id="qcolor_1">Colour</div>
                 <div id="qcolor"><select name="Color" id="Color" class="dropmenu">
        <option value="">Select Color</option>
            </select>

                 </div>
                 <div id="qbrand_1">Brand</div>
                 <div id="qbrand"><select name="Manufacturer" class="dropmenu">
                     <option value="">Any</option> </select>

                 </div>

在getColor.php

The getColor.php

<? $Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%'  AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);

?>
<select name="Color" onChange="getBrand(this.value)">
 <?php  
              echo '<option value="">Any</option>';
             while ($Color = mysql_fetch_array($result)) {
    $selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
    echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?>
</select>

下面是getBrand.php

Here is the getBrand.php

<? $Category= $_GET['Category'];
$Color=$_GET['Color'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Manufacturer FROM products WHERE products.Category LIKE '%$Category%' AND Color = '$Color' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Manufacturer";
$result=mysql_query($query);


?>
<select name="Manufacturer">
<option value="">Select Brand</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['Manufacturer']?>><?=$row['Manufacturer']?></option>
<? } ?>
</select>

这出把颜色正确的,然而,一旦改为选它不会回出范畴值,或者至少当我试图挡在了getBrand文件,它不能被发现的是有没有办法送这个值类别以及对getBrand文件?

This out puts the color correct, however once changed to selected it will not echo out the Category value or at least when I try to get in the the getBrand file it can not be found is there a way to send this value of the Category as well to the getBrand file?

任何帮助的欢迎。

推荐答案

在您getColor.php文件:

In your getColor.php file:

答:你是不是在传递选项标记的价值属性。

A: you are not passing the value attribute in option tag.

B:在 getBrand 功能,你得到的两个变量;一个是的CategoryId 键,另外一个是 Col​​orId ,而在的onchange 功能你逝去的只有一个值, THIS.VALUE

B: In getBrand function you are getting two variables; one is CategoryId and other one is ColorId, but in the Onchange function of Brand select box you are passing only one value, THIS.VALUE.

编辑code:

<? $Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%'  AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);

?>
<select name="Color" onChange="getBrand('<?php echo $Category; ?>',this.value)">
<?php  
          echo '<option value="">Any</option>';
         while ($Color = mysql_fetch_array($result)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';

echo '<option '.$selected2.' value="'.$Color['Color'].'">' . $Color['Color'] . '</option>';
} ?>
</select>