简体   繁体   English

Javascript函数在桌面上可用,在移动浏览器上失败

[英]Javascript function works on Desktop, fails on mobile browser

As a rule, I post on SO only as a LAST resort, when I fail to get a solution from all other sources. 通常,当我无法从所有其他来源获得解决方案时,我只会将这作为最后的手段。 I've been scratching my head over this issue for a whole day now, and considering the plight of some similar questions, I am not very hopeful that I'll get an answer this time. 我已经整天忙于解决这个问题了,考虑到一些类似问题的困境,我不太希望这次能得到答案。

Here is my humble javascript validation function which is called when a button is clicked. 这是我不起眼的javascript验证函数,当单击按钮时会调用该函数。 The buttons when clicked pass an argument ('previous' or 'next') to this function. 单击按钮时,会将参数(“上一个”或“下一个”)传递给该函数。 Now trouble is, this works on all major desktop browsers I tried including IE-11 and Firefox-26. 现在的麻烦是,这在我尝试过的所有主要台式机浏览器(包括IE-11和Firefox-26)上都有效。 However, on ios and WP8, this function is consistently failing. 但是,在ios和WP8上,此功能始终失败。 Nothing happens when the button is clicked (swiped) on mobile (it should either show an alert or move to the next block). 在移动设备上单击(滑动)按钮时,什么都不会发生(它应该显示警报或移至下一个块)。 Here is the code for javascript function: 这是javascript函数的代码:

function validateForm(button){
    smand=mandatory_check();
    if (smand != ''){
        alert('The following fields are mandatory:\n\n' + smand);
        return;
    }        
    if (button=='next'){
        if (scr_curr == (scr_count)){
            smand=mandatory_check(true);
            if (smand != ''){
                alert('The following fields are mandatory:\n\n' + smand);
                return;
            }
            else
                $('#formLawsuit').submit();
        }
        else{
            $('#screen' + scr_curr).addClass('hidden');
            scr_curr+=1;
            $('#screen' + scr_curr).removeClass('hidden');              
            if (scr_curr>1)
                $('#btnPrev').removeClass('hidden');
            if (scr_curr==scr_count)
                showConfirm();
        }
    }
    else{
        $('#screen' + scr_curr).addClass('hidden');
        scr_curr-=1;
        $('#screen' + scr_curr).removeClass('hidden');
        if (scr_curr==1)
            $('#btnPrev').addClass('hidden');
    }
}
function mandatory_check(finale=false){
    var screen='';
    var s='';
    if (!finale)
        screen = '#screen' + scr_curr;
    else
        screen = '#formLawsuit';
    $(screen + ' .control-label').each(function(i) {
        var label = $(this).attr('id');
        var input = '#' + label.replace('label','input');
        //alert(input);
        if ($(input).hasClass('mandatory') && $(input).val()==''){
            s += label.replace('label','') + '\n';
        }
    }        
);
    return s;
}

And these are the two innocent html buttons that call this validate function: 这是调用此验证功能的两个无辜的html按钮:

<div class="form-group">
    <div  style="margin-bottom:120px;" class="col-lg-offset-2 col-lg-6" >
        <button id="btnPrev" name="btnPrev" type="button" class="hidden btn btn-primary btn-lg" onclick="javascript:validateForm('previous')">Previous</button>
        <button id="btnSubmit" name="btnPrev" type="button" class="btn btn-primary btn-lg" onclick="javascript:validateForm('next')">Next</button>
    </div>
</div>

The form and buttons are based on twitter-bootstrap, though I don't think that should make any difference to this. 表单和按钮基于twitter-bootstrap,尽管我认为这不会对此产生任何影响。

Though I did not receive a ready-made solution to this problem, I was pointed in the right direction (all thanks to @Reigel). 尽管我尚未收到针对该问题的现成解决方案,但我的方向正确(都感谢@Reigel)。 Hope this answer helps someone in future looking for similar problems. 希望这个答案可以帮助将来寻找类似问题的人。

There were many errors with my javascript code. 我的JavaScript代码有很多错误。 Unfortunately, most desktop browsers are way too lenient to catch these and as a result we don't get them in testing. 不幸的是,大多数桌面浏览器太宽容了,无法捕获它们,因此我们没有对其进行测试。 Firstly, I had two buttons with the same name attribute (I forgot to change them during the usual copy paste): 首先,我有两个具有相同名称属性的按钮(我忘了在通常的复制粘贴期间更改它们):

    <button id="btnPrev" name="btnPrev" type="button" class="hidden btn btn-primary btn-lg" onclick="javascript:validateForm('previous')">Previous</button>
    <button id="btnSubmit" name="btnPrev" type="button" class="btn btn-primary btn-lg" onclick="javascript:validateForm('next')">Next</button>

Whilst most browsers allow this leniently, turns out IE on WP8 doesn't allow this. 虽然大多数浏览器都宽容地允许这样做,但事实证明WP8上的IE不允许这样做。

Another issue was that I forgot to place block if statements at many places. 另一个问题是我忘记在很多地方放置if语句。 I came to know when I tested my code using http://www.javascriptlint.com/online_lint.php . 我知道何时使用http://www.javascriptlint.com/online_lint.php测试代码。

        if (scr_curr>1)
            $('#btnPrev').removeClass('hidden');
        if (scr_curr==scr_count)
            showConfirm();

After doing these changes, my web app started working fine on WP8 and ios browsers. 完成这些更改后,我的Web应用程序开始在WP8和ios浏览器上正常运行。

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

相关问题 Javascript 适用于桌面,但不适用于移动浏览器 - Javascript works on desktop but not on mobile browser JQM在桌面浏览器上工作而不是在移动设备上 - JQM works on desktop browser not mobile @media查询在桌面浏览器中起作用,而不在移动浏览器中起作用 - @media query works in desktop browser, not in mobile browser Javascript 不适用于移动设备,但适用于桌面设备 - Javascript not working on mobile but works on desktop Href Javascript可在台式机上使用,但不能在移动设备上使用 - Href Javascript works on desktop but not on Mobile Javascript 代码适用于桌面,但不适用于移动设备 - Javascript code works on desktop but not on mobile 使用 Javascript 检测桌面浏览器(非移动设备) - Detect Desktop Browser (not mobile) with Javascript 在单击时关闭Bootstrap Popover:在桌面浏览器中工作正常,但在移动浏览器中工作不正常 - Bootstrap Popover Dismiss on Click: Works fine in desktop browser but not in mobile browser 我的Angular JS应用程序可在桌面浏览器中使用,但不能在移动浏览器中使用 - My Angular JS app works in desktop browser, but not in a mobile browser 导航按钮突出显示在桌面浏览器中有效,但在android移动浏览器中无效 - Nav button highlight works in desktop browser, but not in android mobile browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM