javascript/jquery 更改 location.href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15256514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript/jquery to change location.href
提问by Rafael
This is my HTML
这是我的 HTML
<form id="procurar-novo">
<input type="text" name="procurar" placeholder="Pesquisar no Site" value="">
<input id="procurar-submit" type="button" value="›">
</form>
And this is my jQuery
这是我的 jQuery
<script type="text/javascript">
$(document).ready(function(){
$('#procurar').submit(function(e) {
e.preventDefault();
//edited
window.open = ('http://www.psicotropicus.org/'+'/busca'+encodeURIComponent($('#procurar-submit').val()), '_blank');
return false;
});
});
</script>
The ideai is that by clicking on submit, the javascript/jquery will get the #procurar-submit value and add it on the URL and redirect the user.
想法是通过单击提交,javascript/jquery 将获取 #procurar-submit 值并将其添加到 URL 并重定向用户。
The _blank still not works
_blank 仍然无效
Thanks in advance.
提前致谢。
采纳答案by Kyle Weller
Looks like you don't have an action specified on your form tag. Why not just change the second input element from type=submit
to type=button
. Then you can bind a click event on that button and have full control of what happens next. You don't have to worry about preventing a default submit action. You could do the following:
看起来您没有在表单标签上指定操作。为什么不直接将第二个输入元素从 更改type=submit
为type=button
。然后你可以在那个按钮上绑定一个点击事件,并完全控制接下来发生的事情。您不必担心阻止默认提交操作。您可以执行以下操作:
$(document).ready(function(){
$(document).on('click', '#procurar-submit', function() {
window.location.href = 'http://www.psicotropicus.org/busca'+$('input[name="procurar"]').val();
});
});
To open a new window like on a '_blank' you could change the code to be:
要在“_blank”上打开一个新窗口,您可以将代码更改为:
$(document).ready(function(){
$(document).on('click', '#procurar-submit', function() {
window.open('http://www.psicotropicus.org/busca'+$(input[name="procurar"]).val(), '_blank');
});
});
But be careful with pop-up blockers
但要小心弹出窗口拦截器
EDITI changed the selector that gets the value of the text field. I would maybe add a class or id on that text field so it can be identified apart from others. See this fiddle
编辑我更改了获取文本字段值的选择器。我可能会在该文本字段上添加一个类或 ID,以便可以将其与其他字段区分开来。看到这个小提琴
回答by Rafael
Use window.open with second parameter _blank
使用带有第二个参数 _blank 的 window.open
window.open('url', '_blank');
Try this also:
也试试这个:
<script type="text/javascript">
$(document).ready(function(){
$('#procurar').submit(function(e) {
e.preventDefault();
//edited
window.open = ('http://www.psicotropicus.org/'+'/busca'+encodeURIComponent($('#procurar-submit').val()), '_blank');
return false;
});
});
</script>
Last edit:
最后编辑:
<script>
$(document).ready(function(){
$('form#procurar-novo').submit(function(e) {
//e.preventDefault();
//edited
var url = 'http://www.psicotropicus.org'+'/busca'+ encodeURIComponent('&' + $('input[name=procurar]').val());
window.open(url, '_blank');
return false;
});
});
</script>
<form id="procurar-novo">
<input type="text" name="procurar" placeholder="Pesquisar no Site" value="">
<input id="submitsss" type="submit" value="›">
</form>
Please consider names and ids of the form elements :)
请考虑表单元素的名称和 ID :)