简体   繁体   中英

Javascript/jQuery not working on mobile

I have following javascript code for my site:

$(document).ready(function () {

$('button').attr('disabled', true);

$('button').click(function () {
    $('.registration').addClass('fly-out');

    // Get the variables
    var firstname = document.getElementsByName('firstname')[0].value;
    var lastname = document.getElementsByName('lastname')[0].value;
    var mail = document.getElementsByName('mail')[0].value;

    var informatik = document.getElementById('Informatik').checked ? 1 : 0;
    var medientechnik = document.getElementById('Medientechnik').checked ? 1 : 0;
    var medizintechnik = document.getElementById('Medizintechnik').checked ? 1 : 0;
    var elektronik = document.getElementById('Elektronik').checked ? 1 : 0;
    var fachschule = document.getElementById('Fachschule').checked ? 1 : 0;

    // Send the data to php
    $.ajax({
        url: "add.php",
        type: "POST",
        data: {
            'first': firstname,
            'last': lastname,
            'mail': mail,
            'informatik': informatik,
            'medientechnik': medientechnik,
            'medizintechnik': medizintechnik,
            'elektronik': elektronik,
            'fachschule': fachschule
        }
    });

    // Show the thank you form
    setTimeout(function () {
        $('.registration').addClass('hidden');
        $('.registration').removeClass('fly-out');
        $('.thankyou').removeClass('hidden');


        // Blend out the thank you form
        setTimeout(function () {
            $('.thankyou').addClass('disappear');

            // Show the registration form again
            setTimeout(function () {
                $('.thankyou').addClass('hidden');
                $('.thankyou').removeClass('disappear');

                // Clear the registration form
                $('input[name=firstname]').val('');
                $('input[name=lastname]').val('');
                $('input[name=mail]').val('');
                $('#Informatik').prop('checked', false)
                $('#Medientechnik').prop('checked', false)
                $('#Medizintechnik').prop('checked', false)
                $('#Elektronik').prop('checked', false)
                $('#Elektronik-Fachschule').prop('checked', false)
                $('button').attr('disabled', true);

                $('.registration').removeClass('hidden');
            }, 400);
        }, 2500);
    }, 700);
});
});

function change() {
    var firstname = document.getElementsByName('firstname')[0].value;
    var lastname = document.getElementsByName('lastname')[0].value;
    var mail = document.getElementsByName('mail')[0].value;

    var informatik = document.getElementById('Informatik').checked;
    var medientechnik = document.getElementById('Medientechnik').checked;
    var medizintechnik = document.getElementById('Medizintechnik').checked;
    var elektronik = document.getElementById('Elektronik').checked;
    var fachschule = document.getElementById('Fachschule').checked;

    var result = firstname.length > 0 && lastname.length > 0 && mail.length > 0 && validateMail(mail);
    var result = result && (informatik || medientechnik ||
        medizintechnik || elektronik || fachschule);

    $('button').attr('disabled', !result);
}

function validateMail(mail) {
    var regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return regex.test(mail);
}

It does work just fine on desktop browsers but when I open it with Chrome on Android (6.0 Nexus 5) it does not work (the button is not even disabled).

I now want to know what I did wrong because I need to get this to work on mobile browsers too.

The problem was a rookie mistake on my part...

I embedded jQuery via CDN, not from the local machine. When I tried the code on my phone, I used my notebook as the server. For this to work I had to disconnect from the wireless network. Because of this, jQuery could not be loaded and therefore the code did not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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