ruby - Getting value from search box in javascript - Stack Overflow

How do I get a value from search box in javascript?I'd like to redirect users to particular pages

How do I get a value from search box in javascript? I'd like to redirect users to particular pages based on what they select in the search box. For example, if the user selected "New York" and then clicked on Yes, he'll be redirected to page /about/New York

This work for static pages, but for some reasons I can't get the value from the search box. In Ruby I woud get it with params[:query_name]

I am using hidden_value and tag 'search'

var val = getElementsByTagName('search').value;
if (r) {
  location.href  = "/about/" + val";
}    

How do I get a value from search box in javascript? I'd like to redirect users to particular pages based on what they select in the search box. For example, if the user selected "New York" and then clicked on Yes, he'll be redirected to page /about/New York

This work for static pages, but for some reasons I can't get the value from the search box. In Ruby I woud get it with params[:query_name]

I am using hidden_value and tag 'search'

var val = getElementsByTagName('search').value;
if (r) {
  location.href  = "/about/" + val";
}    
Share Improve this question asked Jun 21, 2012 at 23:23 TyraTyra 5311 gold badge5 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You may also need to specify this == document and use double quotes in case 'search' is a reference to an object, to make sure it is properly interpolated

 var val = document.getElementById("search").value;

or with jQuery:

 var val = $("#search").val();

Did you mean (since I dont think there is an html tag named search):

var val = getElementById('search').value;
if (r) {
  window.location.href  = "/about/" + val;
}    

So above code assumes your element has id set like id="search"


By the way, to select an specific element using getElementsByTagName, you also need to specify index since it returns Node list:

getElementsByTagName('tagName')[indexHERE].value

getElementsByTagName returns an array of many objects based on the tag name, e.g. input,div,a etc. If this is what your search bar looks like:

<input id="search" />

Then do:

getElementById('search').value;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254724a4618886.html

相关推荐

  • ruby - Getting value from search box in javascript - Stack Overflow

    How do I get a value from search box in javascript?I'd like to redirect users to particular pages

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信