简体   繁体   English

JavaScript按钮不再可在移动设备上使用

[英]JavaScript button no longers works on mobile

So I'm troubleshooting an issue in our app and can't figure it out. 因此,我正在对应用程序中的问题进行故障排除,无法解决。 I haven't written the base code and can only inject CSS and Javascript. 我尚未编写基本代码,只能注入CSS和Javascript。 There's a very basic span element with an ID, below that is a snippet of Javascript basically saying "if element with ID submitButton is clicked, submit form #createForm". 有一个具有ID的非常基本的span元素,下面是一段Javascript代码,基本上说“如果单击具有ID SubmitButton的元素,则提交表单#createForm”。 However, on mobile it's broken and the browser is not giving any errors. 但是,在移动设备上它已损坏,浏览器没有给出任何错误。

<form method="post" action="page.html" id="createForm">
    <span id="submitButton">Submit form</span>
</form>
<script> 
    $("#submitButton").on("click", function (event) {
        if (attributeEqualsDisabled($(this).attr('disabled'))) {
            return true;
        }
        $("#submitButton").attr('disabled', true);
        $('#createForm').submit();
    });
</script>

Now, this works perfectly on desktop browsers, even when using the "display as iphone" mode Chrome has. 现在,即使使用Chrome的“显示为iPhone”模式,此功能也可以在桌面浏览器上完美运行。 You can click the button, everything works. 您可以单击按钮,一切正常。

However on mobile safari and when adding the page as a webapp the button no longer works. 但是,在移动浏览器中以及将页面添加为webapp时,该按钮将不再起作用。 When you press it the page just scrolls to the top and does nothing. 当您按下它时,页面只会滚动到顶部,而不会执行任何操作。 I've checked it out through my Mac and everything seems normal and exactly the same as on desktop. 我已经通过Mac进行了检查,一切看起来都很正常,与台式机完全相同。 I can even run $("#submitButton").click(); 我什$("#submitButton").click();可以运行$("#submitButton").click(); on my iphone through the console and it functions perfectly. 通过控制台在我的iPhone上运行,并且功能完美。

There are no errors or warnings in the console. 控制台中没有错误或警告。 Does anyone have any suggestions to troubleshoot this? 有没有人建议解决此问题? I sadly can't give direct access to the code because everything is on an IP locked server. 遗憾的是,我无法直接访问代码,因为所有内容都位于IP锁定的服务器上。

Is there any way of seeing exactly what happens when I click the button? 单击按钮后,是否可以看到确切的结果? I've tried the "Timelines" tab but that shows nothing when I press the button. 我尝试了“时间轴”选项卡,但是当我按下按钮时什么也没显示。

This answer by another user fixed it: https://stackoverflow.com/a/3026621/3461722 另一个用户的此答案已将其修复: https : //stackoverflow.com/a/3026621/3461722

Thank you to Robin Zigmond for pointing me in the right direction. 感谢Robin Zigmond为我指出正确的方向。 I was thinking that something was preventing the click event from firing, but it was simply not picking up on it because I needed to track the touchstart event. 我当时以为是某种原因阻止了click事件的触发,但是它根本没有实现,因为我需要跟踪touchstart事件。

The linked answer does mention that a better solution was found by adding cursor:pointer; 链接的答案确实提到通过添加cursor:pointer;找到了更好的解决方案cursor:pointer; to the button with CSS, however my element already had that so it obviously didn't work in this case. 到CSS按钮,但是我的元素已经具有该功能,因此在这种情况下显然不起作用。

Add this function to detect the taps on mobile: 添加此功能以检测手机上的点击:

$('#submitButton').bind( "touchstart", function(e){
    if (attributeEqualsDisabled($(this).attr('disabled'))) {
        return true;
    }
    $("#submitButton").attr('disabled', true);
    $('#createForm').submit();
});

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

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