将键入的值作为参数传递给链接

时间:2016-02-01 16:34:16

标签: jquery

尝试使用相同的文本构建参数网址已在此文本框中输入。不工作是为什么这样做更好?

<!DOCTYPE html>
<html>
  <head>
    <script src="jquery.js"></script>
    <script>
      $("#texto").on('keypress change', function(event) {
        var data=$(this).val();
        $("#go").text(data);
      });
    </script>
  </head>
  <body>
    <input type="text" value="" id="texto"/>
    <a href="home.php?where=<div id='go'></div>">Home</a>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

您不能将div放在href属性中。我想你所追求的是:

$("#texto").on('keyup change', function(event) {
  $(this).next('a').attr('href','home.php?where=' + $(this).val());
});

<强> jsFiddle example

答案 1 :(得分:0)

您需要使用输入值设置attr a tag。 Html不允许在任何标记的attribute内放置任何元素。

&#13;
&#13;
$(function(){
  $("#texto").keyup(function(){
    $("#go").attr('href','home.php?where=' + $(this).val());
  });
});
&#13;
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>
  <body>
    <input type="text" value="" id="texto"/>
    <a id='go' href="#">Home</a>
  </body>
</html>
&#13;
&#13;
&#13;

相关问题