![](/img/trans.png)
[英]Redirect to asp page using jQuery on a onClick="window.location.href"
[英]Not getting varibles value while using window.location.href in jquery using laravel
碼:
<script>
$(document).ready(function(){
$("#start-date-1").datepicker();
$("#end-date-1").datepicker();
$("#book_now").click(function(e){
e.preventDefault();
locations = $("#location").val();
start_date = $("#start-date-1").val();
end_date = $("#end-date-1").val();
guests = $("#guests").val();
if(locations=='' && start_date!='' && end_date!='' && guests!='')
{
$("#location").addClass("red_border");
}
else if(locations!='' && start_date!='' && end_date!='' && guests!='')
{
window.location.href="{{URL::to('s?location="+locations+"')}}";
}
});
});
</script>
在這段代碼中,我只是簡單地獲取locations, start_date, end_date, guests
值locations, start_date, end_date, guests
和所有變量值都顯示在警報中,但是當我單擊book_now
它使用window.location.href
重定向了我,但是查詢字符串中的locations
值為無法正確顯示。
它是http://localhost/luxvacationrentalhomes.com/s?location="+locations+"
它應該是http://localhost/luxvacationrentalhomes.com/s?location=2
。
我怎樣才能解決這個問題?
<script>
$(document).ready(function(){
$("#start-date-1").datepicker();
$("#end-date-1").datepicker();
$("#book_now").click(function(e){
e.preventDefault();
locations = $("#location").val();
start_date = $("#start-date-1").val();
end_date = $("#end-date-1").val();
guests = $("#guests").val();
if(locations=='' && start_date!='' && end_date!='' && guests!='')
{
$("#location").addClass("red_border");
}
else if(locations!='' && start_date!='' && end_date!='' && guests!='')
{
window.location.href="{{URL::to('s')}}" + "?location=" + locations;
}
});
});
</script>
window.location.href="{{URL::to('s?location="+locations+"')}}";
您在這里混合了前端/后端參考框架。 {{}}
內容均由后端的模板引擎處理。 您在javascript中打開"
,然后在模板引擎中將其關閉,這是行不通的。
我使用javascript字符串替換來解決類似的問題,方法是使用后端在路由上生成包含占位符的url,然后用javascript將實際值替換為前端上的url。
window.location.href="{{URL::to('s?location=ReplaceMeWithLocation')}}"
.replace('ReplaceMeWithLocation', location);
另外,您可以使用字符串模板而不是占位符,但是原理是相同的。
window.location.href=`{{URL::to('s?location=${location}')}}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.