2013-04-24 90 views
0

我試圖做的是以下內容。jQuery - 根據其值設置選擇的下拉列表索引

有人通過GET發送表單到我的結果頁面。當結果提交頁面,網址看起來像這樣的例子

index.php?Reiseziel=Italy 

現在,那裏有一個選擇與ID #filtercountry,它包含了所有可用過濾該國。當設置GET值Reiseziel時,我想迭代通過選擇的值並選擇正確的選項。

例如,選擇含有

<select name="Reiseziel" id="filtercountry"> 
<option value="Please choose..." /> 
<option value="Germany" /> 
<option value="Italy" /> 
<option value="Spain" /> 

URL中包含「的index.php?Reiseziel =意大利」,所以我想設置的選項值「意大利製造」的選擇。我將如何實現這一目標?

+0

http://stackoverflow.com/a/13644610/1081079的 – freshbm 2013-04-24 11:28:32

+0

可能重複的[jQuery的查詢字符串](http://stackoverflow.com/questions/3788125/jquery-querystring) – nhahtdh 2013-04-24 12:36:20

回答

0

使用此功能described here

function GetURLParameter(sParam) 
{ 
    var sPageURL = window.location.search.substring(1); 
    var sURLVariables = sPageURL.split('&'); 
    for (var i = 0; i < sURLVariables.length; i++) 
    { 
     var sParameterName = sURLVariables[i].split('='); 
     if (sParameterName[0] == sParam) 
     { 
      return sParameterName[1]; 
     } 
    } 
}​ 

然後,您可以使用它像這樣:

var reiseziel= GetURLParameter('Reiseziel'); 

然後設置使用變量選擇的值。

0

使用此功能來從查詢字符串值,它使用RegExp

function getParameter(name) 
{ 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.search); 
    if(results == null) 
    return ""; 
    else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

,然後設置下拉列表中這樣的值:

$('#filtercountry').val(getParameter('Reiseziel')); 

或本:

var reiseziel = getParameter('Reiseziel'); 
$('#filtercountry').val(reiseziel); 
0
function QueryString(key) { 
       fullQs = window.location.search.substring(1); 
       qsParamsArray = fullQs.split('&'); 
       for (i = 0; i < qsParamsArray.length; i++) { 
        strKey = qsParamsArray[i].split("="); 
        if (strKey[0] == key) { return strKey[1]; } 
       } 
      } 

var Reisezielnew = QueryString("Reiseziel"); 

你會得到country nameReisezielnew,你可以使用它,因爲你想要的。

0
function getQueryStringFromUrl() 
{ 
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for(var i = 0; i < hashes.length; i++) 
    { 
     hash = hashes[i].split('='); 
     vars.push(hash[0]); 
     vars[hash[0]] = hash[1]; 
    } 
    return vars; 
} 

$(document).ready(function() { 
     var name = getQueryStringFromUrl("Reiseziel"); 
     $('#dropdownid').val(name); 
    }); 
相關問題