简体   繁体   English

javascript获取文本框的值

[英]javascript getting the value of a text box

I have this text box here... 我这里有这个文本框......

<input name="search" type="text" maxlength="512" id="search" class="searchField" autocomplete="off" title="" />

and I also have this submit 我也有这个提交

<input type="submit" name="btnSearch" value="Search" onclick="location.href='http://www.website.com/search/';" id="btnSearch" class="buttonSearch" />

what I am trying to do is add whatever is in the text box in my 我想要做的是添加我的文本框中的内容

onclick="location.href='http://www.website.com/search/';"

so it would look like this.. 所以它看起来像这样..

onclick="location.href='http://www.website.com/search/what ever the user searches';"

how would I go about doing this, I have been googling my little heart out. 我怎么会这样做,我一直在谷歌上搜索我的小心脏。

Please avoid mixing JavaScript and HTML. 请避免混合使用JavaScript和HTML。 You can remove onclick attribute and replace it with this in plain JavaScript somewhere after the DOM has loaded: 您可以删除onclick属性,并在DOM加载后在纯JavaScript中将其替换为:

document.getElementById('btnSearch').onclick = function() {
    var search = document.getElementById('search').value;
    var searchEncoded = encodeURIComponent(search);
    window.location.url = "http://www.website.com/search/" + searchEncoded;
}

Also remember about escaping the search box, eg using encodeURIComponent() . 还要记住转义搜索框,例如使用encodeURIComponent() Here is a working jsfiddle example . 这是一个有效的jsfiddle示例

This should work: 这应该工作:

onclick="location.href='http://www.website.com/search/'+document.getElementById('search').value;"

But I wouldn't ever write that in one of my project as writing script directly on tags is a bad practice. 但我不会在我的一个项目中写这个,因为直接在标签上编写脚本是一种不好的做法。

Here is a working jsfiddle 这是一个工作的jsfiddle

I moved the event handler out of the button as it is more maintainable. 我将事件处理程序移出按钮,因为它更易于维护。 Also I encode the search query so that it gets to the server properly. 此外,我编码搜索查询,以便它正确到达服务器。

var search = document.getElementById('search');
var submit = document.getElementById('btnSearch');

submit.addEventListener('click', function(e) {
    var searchValue = encodeURIComponent(search.value);  // encode the search query
    window.location.href = 'http://www.website.com/search/' + searchValue ;
});

You can add it to the onclick event like so 您可以将其添加到onclick事件中

document.getEelementById("btnSearch").onclick = function(){
    location.href='http://www.website.com/search/' + document.getEelementById("search").value;
} 

edit: aaaaand too slow... oh well. 编辑:aaaaand太慢了......哦。 At least this is not inline. 至少这不是内联的。

You would be better off using the < script> tag for this task. 最好使用<script>标记执行此任务。 Example: 例:

<input name="search" type="text" maxlength="512" id="search" class="searchField" autocomplete="off" title="" />
...
<input type="submit" name="btnSearch" value="Search"  id="btnSearch" class="buttonSearch" />
<script type="text/javascript">
var button= document.getElementById('btnSearch');
button.onclick= function(){
    var text= document.getElementById('search').value;
    location.href='http://www.website.com/search/'+text;
}
</script>

However, you should try to 'clean' a little the text from the textbox so when you append it to the url you get a valid url. 但是,您应该尝试从文本框中“清理”一些文本,以便在将其附加到网址时获得有效的网址。 You should trim the text, then search for special characters and escape them, etc. 您应修剪文本,然后搜索特殊字符并转义它们等。

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

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