我想在用户从下拉列表中选择时将查询参数添加到URL
我在<select></select>
<div class="filter-select">
<select id="orders-filter" onchange="sortOrders(this.value)">
<option value="" disabled selected>Sort order by</option>
<option name="sortBy" value="sort=ordered">Ordered</option>
<option name="sortBy" value="sort=approved">Approved</option>
<option name="sortBy" value="sort=processing">Processing</option>
</select>
</div>
最初,URL将具有默认参数,我想将新参数添加到URL
http://localhost:8000/manager/orders?outlet_id=1
function sortOrders(parameter) {
var url = window.location.href;
if (url.indexOf('?') > -1) {
if(url.indexOf('sort') > -1) {
}
url += '&'+parameter;
} else {
url += '?'+parameter;
}
window.location.href = url;
}
上面的功能工作正常,但是当我选择不同的参数时,它会不断添加它,我想替换当前的那个
//current result
http://localhost:8000/manager/orders?outlet_id=1&sort=ordered&sort=approved&sort=processing
//expected result
http://localhost:8000/manager/orders?outlet_id=1&sort=ordered
http://localhost:8000/manager/orders?outlet_id=1&sort=approved
http://localhost:8000/manager/orders?outlet_id=1&sort=processing
我要检查参数是否有sort
关键字,如果不附加参数则替换值
谢谢