简体   繁体   中英

Button with location.href doesn't work in Firefox

I have a textbox for search and a button that redirects to given value of the textbox. It works well except for firefox 3, which ignores the function completely. Any ideas why and how to fix it? I have already tried window.location instead of location.href, but it again works well in all major browsers but firefox.

My code:

<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+search.value" value="Search" />

EDIT

And here is the generated code:

<input id="search" name="search" type="text" value="" /> 
<input type="button" onclick="window.location='/Authorized/Accounts/0/1/'+search.value" value="Search" />

search.value is looking for a JavaScript variable called 'search', not your form field.

You should be using document.getElementById('search').value

<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+document.getElementById('search').value;" value="Search" />

If you're using jQuery:

<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+$("#search").val();" value="Search" />

I'm not sure why your code doesn't work in FF3, but I got it to work by adding the domain to the beginning of the URL and also by removing the leading slash.

So, if it's possible for you to either add the domain or use relative paths in that location, it should work. For example, the generated line might look like this:

<input type="button" onclick="window.location='http://www.example.com/Authorized/Accounts/0/1/'+search.value" value="Search" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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