使用 laravel 在 jquery 中使用 window.location.href

代码:


<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并且所有变量值都显示在警报中,但是当我单击book_now它时会重定向我,window.location.href但locations查询字符串中的值未正确显示。


它是http://localhost/luxvacationrentalhomes.com/s?location=&quot;+locations+&quot;而且应该是http://localhost/luxvacationrentalhomes.com/s?location=2。


我怎样才能解决这个问题?


元芳怎么了
浏览 303回答 2
2回答

梦里花落0921

window.location.href="{{URL::to('s?location="+locations+"')}}";您在这里混合了前端/后端参考框架。里面的一切{{}}都由后端的模板引擎处理。您"在 javascript 中打开一个,然后在模板引擎中关闭它,这不起作用。我使用 javascript 字符串替换来解决类似的问题,即在后端使用路由生成包含占位符的 url,然后使用 javascript 将实际值替换为前端的 url。window.location.href="{{URL::to('s?location=ReplaceMeWithLocation')}}" &nbsp;&nbsp;&nbsp;&nbsp;.replace('ReplaceMeWithLocation',&nbsp;location);或者,您可以使用字符串模板而不是占位符,但原理是相同的。window.location.href=`{{URL::to('s?location=${location}')}}`

慕森卡

<script>&nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; $("#start-date-1").datepicker();&nbsp; &nbsp; &nbsp; &nbsp; $("#end-date-1").datepicker();&nbsp; &nbsp; &nbsp; &nbsp; $("#book_now").click(function(e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locations = $("#location").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start_date = $("#start-date-1").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end_date = $("#end-date-1").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guests = $("#guests").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(locations=='' && start_date!='' && end_date!='' && guests!='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#location").addClass("red_border");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(locations!='' && start_date!='' && end_date!='' && guests!='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href="{{URL::to('s')}}" + "?location=" + locations;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>
打开App,查看更多内容
随时随地看视频慕课网APP