簡體   English   中英

根據從下拉菜單中選擇的選項創建查詢字符串

[英]Creating a querystring based on what option is selected from a drop down menu

我有這個下拉菜單:

<select name="Filter" onchange="applyFilter(this);">
<option name="Item1" id=Item1 value="10.5">Test1</option>
<option name="Item2" id=Item2 value="27">Test2</option>
<option name="Item3" id=Item3 value="31">Test3</option>
</select>

我仍在學習JavaScript,並嘗試編寫一個函數來生成/加載查詢字符串URL,並將所選項目的值作為參數傳遞。 下拉菜單中的每個選項都需要具有自己的ID。 這是我到目前為止的代碼:

<script language="javascript1.2" type="text/javascript">
function applyFilter()
{ 
var currentQS = '';
var currentObject;
var currentURL = '' + window.location.href;

currentQS = unescape(window.location.search);
var newQS = '';

currentObject = document.getElementById('Item1');
newQS = $Querystring(newQS).set(currentObject.name,currentObject.value).toString();
newQS = newQS.substring(1,newQS.length);

currentObject = document.getElementById('Item2');
newQS = $Querystring(newQS).set(currentObject.name,currentObject.value).toString();
newQS = newQS.substring(1,newQS.length);

currentObject = document.getElementById('Item3');
newQS = $Querystring(newQS).set(currentObject.name,currentObject.value).toString();
newQS = newQS.substring(1,newQS.length);

var newURL = 'http://' + location.hostname + location.pathname + '?' + newQS;
window.location = newURL;
}
</script>

任何幫助,將不勝感激。

不確定$Querystring指的是什么,但是您應該可以這樣做:

document.getElementsByName('Filter')[0].addEventListener('change', function() {
    window.location = 'http://' + location.hostname + location.pathname + '?' + this.name + '=' + this.options[this.selectedIndex].value;
});

但是,選擇菜單可能不是您要執行的操作的最佳選擇。 看看這篇博客文章: http : //uxmovement.com/forms/stop-misusing-select-menus/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM