为什么这个javascript没有重定向?

时间:2021-12-12 04:08:15

I want to have the user click on a button and redirect him/her to a different site I have used the window.location before but for some reason it's not working this time. I'm sure the selector is correct as if I change window.location to alert it alerts every time, so I can't figure what I'm doing wrong.

我想让用户点击一个按钮并将他/她重定向到我之前使用过window.location的其他网站,但由于某种原因,这次它不能正常工作。我确定选择器是正确的,好像我改变window.location每次都提醒它警告,所以我无法弄清楚我做错了什么。

$('#show').click(function(){
        if($('select[name="customers"]').val() == '')
            window.location.replace = 'http://google.com/';
    });

5 个解决方案

#1


3  

You have to use either

你必须使用其中之一

window.location.replace("http://google.com"); // This is like http redirect

or

window.location.href = "http://google.com"; // This is like a link

You can't do window.location.replace= "http://google.com";

你不能做window.location.replace =“http://google.com”;

#2


1  

its a method, not a property.

它是一种方法,而不是财产。

window.location.replace('http://google.com/');

#3


1  

Shouldn't that be windows.location.href = ...?

不应该是windows.location.href = ...?

#4


1  

Set just window.location.href. Like this:

只设置window.location.href。喜欢这个:

window.location.href = 'http://google.com/';

#5


0  

try: window.location = 'http://google.com'

尝试:window.location ='http://google.com'

#1


3  

You have to use either

你必须使用其中之一

window.location.replace("http://google.com"); // This is like http redirect

or

window.location.href = "http://google.com"; // This is like a link

You can't do window.location.replace= "http://google.com";

你不能做window.location.replace =“http://google.com”;

#2


1  

its a method, not a property.

它是一种方法,而不是财产。

window.location.replace('http://google.com/');

#3


1  

Shouldn't that be windows.location.href = ...?

不应该是windows.location.href = ...?

#4


1  

Set just window.location.href. Like this:

只设置window.location.href。喜欢这个:

window.location.href = 'http://google.com/';

#5


0  

try: window.location = 'http://google.com'

尝试:window.location ='http://google.com'