【问题标题】:Creating a querystring based on what option is selected from a drop down menu根据从下拉菜单中选择的选项创建查询字符串
【发布时间】:2013-02-10 19:44:20
【问题描述】:

我有这个下拉菜单:

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

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: javascript drop-down-menu parameters query-string


    【解决方案1】:

    不确定$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/

    【讨论】:

    • 感谢您的链接。不幸的是,作为业务要求的一部分,它必须是一个选择菜单,但我将保留此链接以供将来参考。我尝试了您对脚本的建议,但不幸的是它不起作用。我又对其进行了一次破解,并且能够对其进行一些修改。
    • $('#categoryFilter').change(function() { window.location = 'http://' + location.hostname + location.pathname + '?' + $("#categoryFilter option:selected").attr('id') + '=' + $("#categoryFilter option:selected").val(); }); 剩下的问题是如何在页面加载时将第一个选项设置为默认值。
    猜你喜欢
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多