2017-06-05 46 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