▽1
●
Cherenkov ●80ポイント ベストアンサー |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://jqueryui.com/jquery-1.7.1.js"></script> <script> $(function() { $('#input-up').keyup(function() { $('.qselect').attr('href', './search.php?que=' + $(this).val()); }); }); </script> </head> <body> <form> <input type="text" id="input-up" /> <a class="qselect" class="button" href="./search.php?que=">送信</a> </form> </body> </html>
ユーザーによるテキストフィールドへの入力は全てキー入力であるとは限らない(コンテキストメニューから貼り付けなど)ので、keyupイベントではなくchangeイベントを監視した方がよいと思います。
また、文字列をURIComponentとして埋め込むときはencodeURIComponentでエスケープしましょう(テキストフィールドに「&」や「#」を含む語を入力されたとき、クエリがおかしくなります)。
<!DOCTYPE html> <html> <head> <title>test</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script> <script> $(function () { $('#input-up').change(function () { $this = $(this); $this.next('.qselect').attr('href', './search.php?que=' + encodeURIComponent($this.val())); }); }); </script> </head> <body> <p> <input type="text" id="input-up"> <a href="./search.php" class="qselect button">送信</a> </p> </body> </html>
ところで、JSを使わず、submitボタンの見た目をCSSでいじるというのは駄目でしょうか。