javascript - How to add input(text) value into the href attribute of a link - Stack Overflow

So I have been having a problen that's been bothering for the past couple of hours. I have a searc

So I have been having a problen that's been bothering for the past couple of hours. I have a search input where after the user enters the input and presses send, the text should be added into the href attribute and sent to the results page where it will be used to query the database. I can't figure out a way to get it to work and if I could get help from any of you it would be really appreciated. I know how to get the search to work using ajax or just using ordinary html form so please don't suggest that. I just need it to work the way I asked, it's a long story :-). Thanks in advance. Here's an excerpt

<input type="text" class="form-control" id="search-text" placeholder="Search...">
<a href="results.php?search=WHERE-I-WANT-THE-INPUT-TO-GO" class="btn btn-default" id="send">Search</a>

So I have been having a problen that's been bothering for the past couple of hours. I have a search input where after the user enters the input and presses send, the text should be added into the href attribute and sent to the results page where it will be used to query the database. I can't figure out a way to get it to work and if I could get help from any of you it would be really appreciated. I know how to get the search to work using ajax or just using ordinary html form so please don't suggest that. I just need it to work the way I asked, it's a long story :-). Thanks in advance. Here's an excerpt

<input type="text" class="form-control" id="search-text" placeholder="Search...">
<a href="results.php?search=WHERE-I-WANT-THE-INPUT-TO-GO" class="btn btn-default" id="send">Search</a>
Share Improve this question asked Jun 12, 2015 at 16:47 user4909046user4909046 1
  • Might need more info (like the code for your <form>) but you should probably set it up as a form with method='get' and action='newurl' rather than mess with jquery or javascript – Michael Tallino Commented Jun 12, 2015 at 16:53
Add a ment  | 

2 Answers 2

Reset to default 3

update

$("#search-text").on("keyup",function(e){
  if(e. keyCode === 13){
    var address = $('#send').attr('href');
    window.location = address;
  } 

  $('#send').attr("href","results.php?search="+$(this).val());
})

You can do something like this

$('#send').on('click', function(e){
  e.preventDefault();
  var address = $(this).attr('href') + $('#search-text').val();
  window.location = address;
});

Provided the href looks like

<a href="results.php?" ... />

Improving @toby's solution a bit. Instead of keypress use keyup

$("#search-text").on("keyup",function(){
  $('#send').attr("href","results.php?search="+$(this).val());
})
var $anchor = $("#send");
$("#search-text").on("keypress",function(){
    $anchor.attr("href","results.php?search="+$(this).val());
})

This should do the trick

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信