如何改变url没有刷新或重新加载页面时,单选按钮选中


How to change the url without refreshing or reloading page on when radio button checked?

问题:我想在不刷新的情况下更改页面的url,但现在当我选中单选按钮时,页面会刷新并更改url。例如,

我的当前页面url是:www.example.com/cart/

当我检查拾取,日期和时间单选按钮,然后页面将刷新并显示url:

www.example.com/cart/? method_choice = pickup&时间= 12 pm-3pm&天=星期二

但我希望页面不会刷新,url会改变,当单选按钮被选中

您想要刷新页面?

// Reloads current page
document.location.reload();
// Go to page
window.location = "http://www.example.com/cart/?method_choice=pickup&time=12PM-3PM&day=tue";

可以使用history.pushState();

例如:

history.pushState('', '', '/cart/?method_choice=pickup&time=12PM-3PM&day=tue');