我正在尝试获取一个选项值并将其添加到标头位置。
<select id="time">
<option value="1"> 1 hour</option>
<option value="2"> 2 hours</option>
</select>
当从上面的列表中选择一个选项时,它应该将其作为get变量添加到当前标头url中。。如
header=www.test.com?时间=2
<select id="time2">
<option value="1"> 1 min</option>
<option value="2"> 2 min</option>
</select>
当前标题为www.test.com时?时间=2如果从该列表中选择了一个值,则应将min=2添加到当前标头中。即www.test.com?时间=2&最小值=2
(有多个选择选项,所以当每个选项都被选中时,我如何将get变量添加到当前标题?
$('time').on('change', function (e)
{?
});
我建议:
// binding an event-handler to the 'change' event (selecting by class-name):
$('.query').change(function(){
// retrieving the current hash (though search is, of course, an alternative):
var query = window.location.hash;
/* if there is a current hash we apppend the name and value of the
current select element to that hash:
*/
window.location.hash = (query.length > 0 ? query + '&' : '') + this.name + this.value;
// verifying the change/showing what was set/changed:
console.log(window.location.hash);
/* triggering the change-handler function, this can be omitted if you don't
want the handler to run on page-load: */
}).change();
再加上稍作修改的HTML:
<select name="hour" class="query">
<option value="1"> 1 hour</option>
<option value="2"> hours</option>
</select>
<select name="mins" class="query">
<option value="1"> 1 min</option>
<option value="2"> 2 min</option>
</select>
JS Fiddle演示。
这会更新window.location.hash
(#time=2
),而不是window.location.search
(?time=2
),因为更新后者会导致页面重新加载(使用新的搜索字符串),而更新hash
会阻止页面重新加载。
但是,如果页面重新加载对您来说不是问题,那么您可以简单地使用window.location.search
而不是window.location.hash
。