如何使用JQUERY在选项框中保留选定的值?

时间:2012-05-14 08:50:03

标签: php ajax jquery-ui 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中,但在页面中值显示为“Select one”?请帮忙排序

2 个答案:

答案 0 :(得分:1)

 $(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]);
            }
        });

答案 1 :(得分:0)

<?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;
  }
?>