我试图获取一个选项值并将其添加到标题位置。
<select id="time">
<option value="1"> 1 hour</option>
<option value="2"> 2 hours</option>
</select>
当从上面的列表中选择一个选项时,它应该将它作为get变量添加到当前标题url ..例如
头= www.test.com?时间= 2
<select id="time2">
<option value="1"> 1 min</option>
<option value="2"> 2 min</option>
</select>
虽然目前的标题是www.test.com?time=2 如果从该列表中选择了一个值,则应将min = 2添加到当前标头中。 即 - www.test.com?time=2&min=2
(有多个选择选项,因此当选择每个选项时,如何将get变量添加到当前标题?
$('time').on('change', function (e)
{?
});
答案 0 :(得分:1)
我建议:
// 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>
这会更新window.location.hash
(#time=2
),而不是window.location.search
(?time=2
),因为更新后者会导致页面重新加载(使用新搜索 - 字符串),而更新hash
会阻止页面重新加载。
但是,如果页面重新加载对您来说不是问题,那么您只需使用window.location.search
代替window.location.hash
。