如何使用 JQUERY 将所选值保留在下拉框中


How to keep selected value in a dropdown box using JQUERY?

嗨,我使用这样的jquery代码

$(".selfont").change(function(event){
$('#dav').val();
window.location ='?davQ=' + $('#dav').val() + '&pathogenQ=' + $('#pathogen').val() + '&topicQ=' + $('#topicF').val() ;
});

我想保留用户在每个下拉框中选择的下拉值。但目前该值不是用户选择的值,它始终显示第一个值。如何使用用户使用 jquery 选择的值设置下拉字段?请帮助我。

我的第一个选择框代码如下所示

<select name="dav" id="dav" style="width: 275px" class='selfont' >
<option value='' class=''>Select one</option>
                        <?php
                                $test = mysql_query("SELECT DISTINCT DataVersion FROM olivesdeptable ORDER BY DataVersion DESC");
                                 $i=1;
                                 while($numval=mysql_fetch_array($test))
                                 {
                                          print "<option value='"".$numval['DataVersion']."'">".$numval['DataVersion']."</option>";
                                          $i=$i+1;
                                  }
                          ?>
                    </select>

即使我们选择值,它也会在字段中显示为"选择一个"。

下拉字段的 JavaScript 代码

<script type="text/javascript">
if (document.getElementById("dav").selectedIndex < 1)
{
document.getElementById('pathogen').selectedIndex = "";
document.getElementById('pathogen').disabled = true;
}
if (document.getElementById("pathogen").selectedIndex < 1)
{
document.getElementById('topicF').selectedIndex = "";
document.getElementById('topicF').disabled = true;
}
if (document.getElementById("topicF").selectedIndex < 1)
{
document.getElementById('ind').selectedIndex = "";
document.getElementById('ind').disabled = true;
}
if (document.getElementById("ind").selectedIndex < 1)
{
document.getElementById('subind').selectedIndex = "";
document.getElementById('subind').disabled = true;
}
if (document.getElementById("subind").selectedIndex < 1)
{
document.getElementById('countryR').selectedIndex = "";
document.getElementById('countryRF').options.length = 0;
document.getElementById('countryRF').selectedIndex = "";
document.getElementById('countryR').disabled = true;
document.getElementById('countryRF').disabled = true;
}
</script>

即使值已更新,第二个下拉框也显示为禁用?

下一个下拉字段标记如下所示

 <select name="pathogen" id="pathogen" style="width: 275px" class='selfont' >
    <option value=''>Select one</option>
                            <?php
                                    $test = mysql_query("SELECT DISTINCT Pathogen FROM olivesdeptable where DataVersion='$davQ' ORDER BY Pathogen ASC");
                                    $i=1;
                                     while($numval=mysql_fetch_array($test))
                                     {
                                              print "<option value='"".$numval['Pathogen']."'">".$numval['Pathogen']."</option>";
                                              $i=$i+1;
                                      }
                              ?>
                    </select>

只有第一个 Dropbox 值适用于下一个 Dropbox 值存储在 URL 中,但在页面中该值显示为"选择一个"?请帮忙排序

 $(document).ready(function () {
            $('#dav').val(getURLParameter('davQ'));
            $('#pathogenQ').val(getURLParameter('pathogenQ'));
            $('#topicQ').val(getURLParameter('topicQ'));
            $(".selfont").change(function (event) {
                 window.location = '?davQ=' + $('#dav').val() + '&pathogenQ=' + $('#pathogen').val() + '&topicQ=' + $('#topicF').val();
            });
            function getURLParameter(name) {
            return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
            }
        });
<?php
  $test = mysql_query("SELECT DISTINCT DataVersion FROM olivesdeptable ORDER BY DataVersion DESC");
  $i=1;
  $selected = '';
  // make compare with the value you want to select with the parameter form url
  // here I assume $_GET['davQ'] holds the value to $numval['DataVersion']
  if($_GET['davQ'] == $numval['DataVersion']) $selected = 'selected';
  while($numval=mysql_fetch_array($test))
  {
     echo "<option value='"".$numval['DataVersion']."'" $selected>".$numval['DataVersion']."</option>";
     $i=$i+1;
  }
?>