简体   繁体   English

Ajax在移动和触摸屏设备上工作异常

[英]Ajax working odd on mobile and touch screen devices

I have a dynamic form in my app that can be live edited (adding or removing input fields) but i had a problem on validation when user made some mistake and didn't fill something right all new added fields would be gone and that was very bad because the user had to recreate everything how he had it before. 我的应用程序中有一个动态表单,可以实时编辑(添加或删除输入字段),但是当用户犯了一些错误并且没有正确填写某些内容时,我在验证时遇到了问题,所有新添加的字段都会消失,这非常不好,因为用户必须重新创建以前的所有内容。

So to solve that problem i have created an ajax call just to check if validation is fine: 因此,为了解决该问题,我创建了一个ajax调用,仅用于检查验证是否正确:

// send ajax first to check validation so that the dynamic fields don't disappear
$(document).on('touchstart mousedown', ':submit', function() {
    $('input[name="items"]').val($('.item').length);
    var form = $("form");
    var myMethod = '';
    var myRoute = '';
    var orderId = "<?php if (isset($order->id)) { echo $order->id; } else { echo false;} ?>";

    if( $(this).val() === 'Make new order' && !orderId){
        myMethod = 'post'
        myRoute = '/orders'

    } else {
        myMethod = 'post';
        myRoute = '/orders/edit/' + orderId;
    }

    $.ajax({
        type: myMethod,
        url: myRoute,
        headers: { 'X-CSRF-TOKEN': "{{csrf_token()}}" },
        data: form.serializeArray(),
        dataType: 'json',
        success: function(data){
            console.log('validation success!');
        },
        error: function(data) {
            var errors = data.responseJSON;
            var errorsArr = [];
            for (error in errors) {
                errorsArr.push(errors[error][0]);
            }
            bootbox.alert(errorsArr.join("<br>"));
            console.log(errors);

        }
    });
});

and in my controller i have this: 在我的控制器中,我有这个:

public function store(OrdersForm $request)
{
    if($request->ajax()){ 

        return response()->json();

    } else {

        // save all...
    }
}

And the problem is that all works fine on the computer i get the validation messages for both create and edit submits but on tablet and mobile phones all crazy things going on... 问题是所有这些东西在计算机上都可以正常工作,但是我收到了用于创建和编辑提交的验证消息,但是在平板电脑和手机上,所有疯狂的事情都在发生...

I get double validation error on create and on edit i get validation message briefly and it goes away i don't know where to debug this...how can i see what is happening on mobile phone? 我在创建和编辑时遇到双重验证错误,我得到验证消息只是短暂的,它消失了,我不知道该在哪里调试...我如何查看手机上正在发生什么?

您的代码之所以这样触发是因为您需要将Submit按钮更改为常规按钮,然后在您发送的AJAX请求检查验证成功返回时手动触发表单提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM