选中的属性阻止将位置更改为路径名

checked Attribute prevents to change location to pathname

本文关键字:位置 路径名 属性      更新时间:2023-09-26

我需要一个按钮,将用户重定向到他所在的同一页面,但不包含querystring。

因此,在使用输入并发送GET请求后,如

www.example.com?id=5

此按钮需要将用户发送到www.example.com。(注意:我不想要<a href="">标签)

我试过了:

在html中<button onclick="window.location.href='index.html'">Reset Data</button>

然后在JS 中

$('button').click(function(){ window.location = window.location.pathname})

但什么都没有。我发现有趣的是,最后一次尝试在js控制台中工作,但在正常脚本中,页面只是刷新(包括查询字符串)

编辑

链接到示例:

https://eaaqdev01.cloudapp.net/test3.html

为了模拟我的服务器代码,我对这个进行了硬编码

<input type="checkbox" name="TBRsubpots[]" value="Other" checked>

即上述输入将已经被检查。基本上,用户在提交后,会看到他检查的输入已经检查过了(如上所述)。如果没有这个,它就会起作用,正如你在上看到的那样

https://eaaqdev01.cloudapp.net/test4.html

这里的问题是button元素充当提交按钮,除非在其type属性中另行指定。因此,它导致您的表单被提交,而您对window.location的更改基本上没有任何效果。

这应该有效:

<button type="button"
        onclick="window.location = window.location.pathname;">reset</button>

或者这个:

<button onclick="return void(window.location = window.location.pathname);">reset</button>

或者,你也应该能够走更好、不引人注目的路线:

$('button').click(function(e) { 
     e.preventDefault(); 
     window.location = window.location.pathname; 
});

这是我认为现在大多数人都会推荐的。在任何情况下,在其上放置type="button"属性仍然是一个好主意。

另请参阅此处的type属性信息。

试试

window.location.href.split("?")[0]

try:

 <button onclick="location.search='';">Reset Data</button>

试试这个

$(location).attr('ref').substr