2017-06-05 44 views
0

我有一個鏈接,將獲得輸入(文本)的值,如果不是空的,並將其附加到href值。有沒有辦法完成它?繼承人我的代碼。提前致謝。所有的如何將輸入類型文本值附加到href laravel?

<a id= "lnk_audio_devices" href="{{route('search.products', ['category' => 'audio_devices', 
      @if(txt_product.value != '') 
      'products' => txt_product.value 
      @endif 
      ])}}" >Audio Devices</a> 

回答

0

首先,你可能想嘗試與形式的解決辦法,那就是通常的方式這樣的事情都做了,導致這樣的foobar.com/foo/bar?yourinput=whatyoutyped URL。它不是按鈕是鏈接,而是nom提交表單。

如果你想保持你的laravel路由相同。當點擊按鈕時,您需要使用javascript將文本值附加到鏈接。

function navigate(link, inputid){ 
 
    var url = link.href + "/" + document.getElementById(inputid).value; 
 
    //window.location.href = url; //navigates to the given url, disabled for demo 
 
    alert(url); 
 
}
<a href="" onClick="navigate(this,'myText')">click me</a> 
 
<input type="text" id="myText">

+0

謝謝您的回答。也許是因爲我想在我的href中使用'路由',這就是爲什麼我不能正確地使用它。我嘗試你的代碼,但問題是它總是將重定向到相同的鏈接不在所需的網址。我認爲它是因爲href =「」。第二個是如果URL中有參數,它總是得到它..所以我嘗試修改你的代碼。 – user3418036

+0

這是我的新代碼..function導航(類別){VAR = text_search的document.getElementById( '搜索')value.trim()。 text_search =(text_search =='')? '':'&product ='+ text_search; category ='?category ='+ category; var url = window.location.origin +'/'+ category + text_search; window.location.href = url; } – user3418036