JavaScript 点击重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12802482/
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 onclick redirect
提问by user1269625
I have this text field and button here
我在这里有这个文本字段和按钮
<input name="txtSearch" type="text" id="txtSearch" class="field" />
<input type="submit" name="btnSearch" value="" id="btnSearch" class="btn" onclick="javascript:SubmitFrm()" />
and when the user clicks on the submit button this function is suppose to run
当用户点击提交按钮时,这个函数应该运行
<script type="text/javascript">
function SubmitFrm(){
var Searchtxt = document.getElementById("txtSearch").value();
window.location = "http://www.example.com/search/?Query=" + Searchtxt;
}
</script>
But nothing happens, what I expecting to happen is when the user clicks on the submit button, take the value from the search text box and redirect the user to the url + the value of the search text box...
但是没有任何反应,我期望发生的是当用户单击提交按钮时,从搜索文本框中获取值并将用户重定向到 url + 搜索文本框的值...
What am I doing wrong?
我究竟做错了什么?
采纳答案by user1269625
Doing this fixed my issue
这样做解决了我的问题
<script type="text/javascript">
function SubmitFrm(){
var Searchtxt = document.getElementById("txtSearch").value;
window.location = "http://www.mysite.com/search/?Query=" + Searchtxt;
}
</script>
I changed .value();
to .value;
taking out the ()
我改.value();
到.value;
取出()
I did not change anything in my text field or submit button
我没有更改文本字段或提交按钮中的任何内容
<input name="txtSearch" type="text" id="txtSearch" class="field" />
<input type="submit" name="btnSearch" value="" id="btnSearch" class="btn" onclick="javascript:SubmitFrm()" />
Works like a charm.
奇迹般有效。
回答by Frédéric Hamidi
There are several issues in your code :
您的代码中有几个问题:
You are handling the
click
event of a submit button, whose default behavior is to post a request to the server and reload the page. You have to inhibit this behavior by returningfalse
from your handler:onclick="SubmitFrm(); return false;"
value
cannot be called because it is a property, not a method:var Searchtxt = document.getElementById("txtSearch").value;
The search query you are sending in the query string has to be encoded:
window.location = "http://www.mysite.com/search/?Query=" + encodeURIComponent(Searchtxt);
您正在处理
click
提交按钮的事件,其默认行为是向服务器发送请求并重新加载页面。您必须通过false
从处理程序返回来抑制这种行为:onclick="SubmitFrm(); return false;"
value
不能被调用,因为它是一个属性,而不是一个方法:var Searchtxt = document.getElementById("txtSearch").value;
您在查询字符串中发送的搜索查询必须进行编码:
window.location = "http://www.mysite.com/search/?Query=" + encodeURIComponent(Searchtxt);
回答by Ian
Change the onclick from
将点击更改为
onclick="javascript:SubmitFrm()"
to
到
onclick="SubmitFrm()"
回答by Philipp
Just do
做就是了
onclick="SubmitFrm"
The javascript:
prefix is only required for link URLs.
该javascript:
前缀仅需要链接的URL。
回答by Marijke Luttekes
Remove 'javascript:' from your code and it should work.
从您的代码中删除 'javascript:' ,它应该可以工作。
Do you happen to use FireFox? I have learned from someone else that FireFox no longer accepts the 'javascript:' string. However, for the life of me, I cannot find the original source (though I believe it was somewhere in FF update notes).
你碰巧使用 FireFox 吗?我从其他人那里了解到 FireFox 不再接受 'javascript:' 字符串。但是,对于我的一生,我找不到原始来源(尽管我相信它在 FF 更新说明中的某个地方)。