简体   繁体   English

javascript window.location.href 不断添加到 url

[英]javascript window.location.href keeps adding to url

I have a simple dropdown that forwards to a new url onChange.我有一个简单的下拉列表,它转发到一个新的 url onChange。

It looks like this:它看起来像这样:

function changeCompanyType(companyType) {
      window.location.href = 'type/'+companyType+'/';

    }

The first change works great and goes to the url:第一个更改效果很好,并转到了 url:

http://127.0.0.1/companies/type/bank/ http://127.0.0.1/companys/type/bank/

The next time I click on the dropdown from the new page it adds type and the company type again:下次我点击新页面的下拉菜单时,它会再次添加类型和公司类型:

http://127.0.0.1/companies/type/bank/type/hosptial/ http://127.0.0.1/companies/type/bank/type/hospial/

How can I just have the company type changed so the url doesn't keep being added to?我怎样才能改变公司类型,这样网址就不会一直被添加到?

Prefix with a forward slash ( / )带正斜杠 ( / ) 的前缀

window.location.href = '/type/'+companyType+'/';
                        ^ -- a slash to make an absolute url

// or (depends on what you want to do)
window.location.href = '/companies/type/'+companyType+'/';
                        ^ -- a slash to make an absolute url

Add a '/' at the front of the link like this像这样在链接前面添加一个'/'

function changeCompanyType(companyType) {
  window.location.href = '/type/'+companyType+'/';
}

Use:用:

function changeCompanyType(companyType) {
    window.location.href = '/type/'+companyType+'/';

}

Add line:添加行:

companyType = '';

after you have set the href.设置href后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM