【问题标题】:Laravel 6.* Blade: Button Onclick Event not Invoking Javascript at the bottom of the pageLaravel 6.* Blade:按钮 Onclick 事件未在页面底部调用 Javascript
【发布时间】:2020-03-09 12:12:36
【问题描述】:

我有一个带有两个隐藏输入字段的表单来保存尝试提交表单的当前用户的经度和纬度,我现在在刀片文件下面编写了 javascript 来设置获取当前 gps 位置并将其设置为两个字段,因此它可以与表单一起提交,但永远不会调用方法。请问可能的问题是什么,我使用的是 Laravel 6.*

<script type="application/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>

<script type="application/javascript">
    $(document).ready(function() {
        var y = document.getElementById("lat");
        var z = document.getElementById("lng");


        function getLocation() {
            if ("geolocation" in broswer) {
                alert(navigator.geolocation)
                navigator.geolocation.getCurrentPosition(showPosition, showError);
            } else {
                alert("System");
            }
        }


        function showPosition(position) {
            y.innerHtml.value = position.coords.latitude;
            z.innerHtml.value = position.coords.longitude;
        }

        function showError(error) {
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    alert("Permission Denied")
                    break;
                case error.POSITION_UNAVAILABLE:
                    alert("Position not available")
                    break;
                case error.TIMEOUT:
                    alert("Timed out")
                    break;
                case error.UNKNOWN_ERROR:
                    alert("Unknown Error")
                    break;
            }
        }
    })

</script>

<form class="form" method="PUT" action="{{ route('newBid') }}">
    @csrf
    <input type="number" class="form-control" name="amount" placeholder="0.00" required /><br/>
    <input type="hidden" name="auction_id" value="{{ $auction->id }}" />
    <button type="submit" onclick="getLocation()" class="btn btn-danger btn-block">
        {{ __('Bid') }}
    </button>
    <input type="hidden" id="lat" value="">
    <input type="hidden" id="lng" value="">
</form>

【问题讨论】:

  • 这到底与 Laravel 或 PHP 有什么关系?由于有多个 JS 函数,其中哪些没有被调用?您尝试过什么调试问题?
  • 如果您知道它使用的是纯 html,您尝试检查哪些差异?请不要对试图帮助你的人无礼

标签: javascript php laravel laravel-blade


【解决方案1】:

通常,发生这种情况时,是因为在触发 de onclick 之前提交了表单。尝试将submit 的类型更改为button,然后在getLocation 函数的末尾,调用表单的submit 函数

<script type="application/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>

    <script type="application/javascript">
        $(document).ready(function() {
            var y = document.getElementById("lat");
            var z = document.getElementById("lng");


            function getLocation() {
                if ("geolocation" in broswer) {
                    alert(navigator.geolocation)
                    navigator.geolocation.getCurrentPosition(showPosition, showError);
                } else {
                    alert("System");
                }
                // we submit the form manually
                $('.form').submit();
            }        


            function showPosition(position) {
                y.innerHtml.value = position.coords.latitude;
                z.innerHtml.value = position.coords.longitude;
            }

            function showError(error) {
                switch (error.code) {
                    case error.PERMISSION_DENIED:
                    alert("Permission Denied")   
                        break;
                    case error.POSITION_UNAVAILABLE:
                    alert("Position not available")   
                        break;
                    case error.TIMEOUT:
                    alert("Timed out")   
                        break;
                    case error.UNKNOWN_ERROR:
                    alert("Unknown Error")   
                        break;
                }
            }
        })

    </script>

<form class="form" method="PUT" action="{{ route('newBid') }}">@csrf
                                                <input type="number" class="form-control" name="amount" placeholder="0.00" required /><br/>
                                                <input type="hidden" name="auction_id" value="{{ $auction->id }}" />
                                                <!-- we changed the type to button -->
                                                <button type="button" onclick="getLocation()" class="btn btn-danger btn-block">
                                                    {{ __('Bid') }}


                                                </button>
                                                <input type="hidden"  id="lat" value="">
                                                <input type="hidden" id="lng" value="">
                                            </form>

【讨论】:

  • 我使用 laravel UI 来生成我的界面,引导程序和 VUE ui 是 laravel 6 的新功能,这可能是原因吗?有人在 laravel 6 的页面中成功调用了 javascript 吗?
猜你喜欢
  • 1970-01-01
  • 2020-10-18
  • 2015-04-27
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
相关资源
最近更新 更多