简体   繁体   中英

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

I have this 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>

I'm still learning javascript and am trying to write a function that generates/loads the querystring URL and pass the value of the selected item as a parameter. Each option in the drop down menu will need to have their own ID. This is the code I have so far:

<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>

Any help with this will is appreciated.

Not sure what $Querystring refers to, but you should be able to do it like this:

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

However, a select menu might not be the best option for what you're trying to do. Take a look at this blog post: http://uxmovement.com/forms/stop-misusing-select-menus/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM