简体   繁体   English

为什么我的表格不能在移动设备上使用?

[英]Why doesn't my form work on mobile devices?

I have a form downloaded from http://reusableforms.com/ that works great on desktop. 我有一个从http://reusableforms.com/下载的表格,该表格在台式机上效果很好。 But the submit button changes to "sending ..." and gets stuck on mobile devices. 但是“提交”按钮变为“正在发送...”,并且卡在了移动设备上。 An error message doesn't display and the page just stops working. 错误消息不会显示,并且页面只会停止工作。 What would cause this problem? 什么会导致此问题? I am putting my javascript code below. 我将我的JavaScript代码放在下面。 Here is the site for reference: http://jasminew.xyz/flash 这是供参考的站点: http : //jasminew.xyz/flash

$(function()
{

$.ajaxSetup({ cache: false }); // or iPhones don't get fresh data

function after_form_submitted(data) 
{
    if(data.result == 'success')
    {
        $('#carouselExampleIndicators').hide();
        $('form#reused_form').hide();
        $('#success_message').show();
        $('#error_message').hide();
    }
    else
    {
        $('#error_message').append('<ul></ul>');

        jQuery.each(data.errors,function(key,val)
        {
            $('#error_message 
ul').append('<li>'+key+':'+val+'</li>');
        });
        $('#success_message').hide();
        $('#error_message').show();

        //reverse the response on the button
        $('button[type="button"]', $form).each(function()
        {
            $btn = $(this);
            label = $btn.prop('orig_label');
            if(label)
            {
                $btn.prop('type','submit' ); 
                $btn.text(label);
                $btn.prop('orig_label','');
            }
        });

    }//else
  }

$('#reused_form').submit(function(e)
  {
    e.preventDefault();

    $form = $(this);
    //show some response on the button
    $('button[type="submit"]', $form).each(function()
    {
        $btn = $(this);
        $btn.prop('type','button' ); 
        $btn.prop('orig_label',$btn.text());
        $btn.text('Sending ...');
    });


                var formdata = new FormData(this);
        $.ajax({
            type: "POST",
            url: 'handler.php',
            data: formdata,
            success: after_form_submitted,
            dataType: 'json' ,
            processData: false,
            contentType: false,
            cache: false,
        });

  });   
});

EDIT 编辑
I have opened remote Safari dev tools and the XHR Post is timing out. 我已经打开了远程Safari开发工具,并且XHR Post正在超时。 I'm not sure what that means. 我不确定那是什么意思。

EDIT 编辑
Okay, the problem is that the form stops working if the two upload fields in the form aren't used on iOS. 好的,问题是,如果未在iOS上使用表单中的两个上载字段,则该表单将停止工作。 Does anyone know a workaround? 有谁知道解决方法?

I reviewed your code and would suggest some minor updates: 我检查了您的代码,并建议进行一些小的更新:

$(function() {
  $.ajaxSetup({
    cache: false
  }); // or iPhones don't get fresh data

  function after_form_submitted(data) {
    if (data.result == 'success') {
      $('#carouselExampleIndicators').hide();
      $('form#reused_form').hide();
      $('#success_message').show();
      $('#error_message').hide();
    } else {
      $('#error_message').append('<ul></ul>');
      $.each(data.errors, function(key, val) {
        $("<li>").html(key + ": " + val).appendTo($('#error_message ul'));
      });
      $('#success_message').hide();
      $('#error_message').show();

      //reverse the response on the button
      $('button[type="button"]', $form).each(function() {
        var $btn = $(this);
        var label = $btn.data('orig-label');
        if (label) {
          $btn.prop('type', 'submit');
          $btn.text(label);
          $btn.data('orig-label', null);
        }
      });
    }
  }

  $('#reused_form').submit(function(e) {
    e.preventDefault();

    var $form = $(this);
    //show some response on the button
    $('button[type="submit"]', $form).each(function() {
      var $btn = $(this);
      $btn.prop('type', 'button');
      $btn.data('orig-label', $btn.text());
      $btn.text('Sending ...');
    });

    var formdata = new FormData($form[0]);
    $.ajax({
      type: "POST",
      url: 'handler.php',
      data: formdata,
      success: after_form_submitted,
      dataType: 'json',
      processData: false,
      contentType: false,
      cache: false,
    });
  });
});

I see nothing that is "wrong", just some syntax cleanup. 我没有发现任何“错误”,只是一些语法清除。 Also this was sort of ambiguous in the creation of FormData, so I switched to a more specific element: $form[0] . 同样, this在创建FormData时有点模棱两可,所以我切换到一个更特定的元素: $form[0]

I adjusted a few other things to be more jQuery like. 我调整了其他一些东西使其更像jQuery。 All functions the same. 所有功能相同。

Also, make sure that Ajax URL path is correct. 另外,请确保Ajax URL路径正确。 You have: 你有:

url: 'handler.php'

Make sure that your can browse to this directly. 确保您可以直接浏览到此。 If the path is not correct, the form will not post properly. 如果路径不正确,则表单将无法正确发布。 It may be better to use a Direct URL Path instead of a relative path. 最好使用直接URL路径而不是相对路径。

If you have access to your Web Server or PHP Logs, you can also check there to see if POSTs to your handler.php script are failing in some way. 如果您有权访问Web服务器或PHP日志,则还可以在此处检查以查看对handler.php脚本的POST是否以某种方式失败。 In Safari, you may want to check Network results and see if it provide more response details. 在Safari中,您可能需要检查“网络结果”,并查看其是否提供更多响应详细信息。

Test it, let me know if it works any better. 测试一下,让我知道它是否更好。

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

相关问题 为什么我的 HTML 复选框在移动设备上不起作用? - Why doesn't my HTML checkbox work on mobile devices? 为什么ngInfiniteScroll(无限滚动)在移动设备上不起作用? - Why ngInfiniteScroll (infinite-scroll) doesn't work on mobile devices? 为什么我的JavaScript表单无法正常工作? - Why doesn't my form with javascript work? onmouseover不适用于android移动设备 - onmouseover doesn't work for android mobile devices 链接在移动/触摸设备上不起作用 - Links doesn't work on mobile / touch devices 如何提交表单而不进行重定向和不使用iframe? (似乎不适用于移动设备) - How can I submit a form without redirecting and without using an iframe? (Doesn't seem to work on mobile devices) 移动响应能力,可在网络上使用,不适用于移动设备 - Mobile-responsiveness, works on the web, doesn't work on mobile devices 在移动设备的狩猎中, <a href=“#” onclick=“return false”>不起作用,为什么?</a> - In mobile devices' safari, <a href=“#” onclick=“return false”> doesn't work, why? 为什么在我的 JS 弹出窗口中,我的 html 表单不起作用? - Why in my JS popup my html form doesn't work? 为什么我的声明不禁止表单提交工作? - Why doesn't my statement to disable form submission work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM