2012-05-14 42 views
0

你好我使用一個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,但在頁面存儲值顯示爲「選擇一個」?請幫忙整理

+1

u需要表現出UR HTML標記,或者U設置提琴對我們來說,http://jsfiddle.net –

+0

感謝respose,我已經更新的問題。 – Gopipuli

+0

網址更新列表框中選擇的值,但在列表框中顯示第一個值「選擇一個」? – Gopipuli

回答

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; 
    } 
?> 
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]); 
      } 
     }); 
+0

非常感謝您的代碼,它的工作正常,但我得到了一個JavaScript代碼運行的領域,我已經在我的問題上面更新過。即使在更新第一個字段後,第二個仍然顯示爲禁用?需要修改嗎?請幫助...提前感謝。 – Gopipuli

+0

還有什麼要做的代碼? – Gopipuli

+0

每次在下拉框中選擇的值都會在window.location參數中更新,並且下一個選擇選項將首先啓用。 – Gopipuli

相關問題