以下のフォームでは、action 属性を変更してフォームを送信します。それはうまくいきます。何が起こるか: 現在の場所がhttp://localhost/search/?mod=all
で、検索用語が 14 の場合、アクションは に変更さhttp://localhost/search/?mod=all&handle=14
れ、ブラウザの URL も変更されます。
しかし、次に検索しようとすると、現在の URL が であるため、 が表示されhttp://localhost/search/?mod=all&handle=14
ますhttp://localhost/search/?mod=all&handle=14&handle=15
。検索ワードごとに延々と続きます。
このすべてで元のURLhttp://localhost/search/?mod=all
を保持する方法を考えてください。
フォームは次のとおりです。
<form method="GET" class="modForm" action="">
<input type="text" placeholder="Search" class="modSearchValue">
<input type="radio" name="text" value="text" class="text" title="Search">
</form>
ここにjqueryがあります:
$('.modForm').submit(function(event) {
var $this = $(this);
var query = $this.find('.modSearchValue').val(); // Use val() instead of attr('value').
var locale = window.location;
if ($('.text').is(':checked')) {
query = '&text=' + query;
} else {
query = '&handle=' + query;
}
route = locale + query;
console.log(route);
if (query.length >= 1) {
// Use URI encoding
var newAction = (route);
console.log(newAction); // DEBUG
// Change action attribute
$this.attr('action', newAction);
//event.preventDefault();
} else {
console.log('Invalid search terms'); // DEBUG
// Do not submit the form
event.preventDefault();
}
});