El botón Enviar solo funciona cuando los primeros 2 campos están vacíos

http://windowsnaarlinux.nl/#distrokiezer -- Temporarily replaced the page with a PHP version, here's the javascript version: http://windowsnaarlinux.nl/pages/distrokiezer.html


Whenever you leave one of the first two fields empty and click the submit button (captioned Confirmar), you get a warning and the script ends, as it's supposed to do. But when you fill in the first two fields, and then click the submit, nothing what so ever happens. There doesn't pop up any alert, as it should. Neither does there appear any kind of error or warning in Chrome's javascript console.

The original page (the one of wich jQuery loads the content into div.window#content) is located here: http://windowsnaarlinux.nl/pages/distrokiezer.html
Might be worth mentioning that it worked fine before, and that I honestly don't have a clue myself what I did to break it.


... I just noticed I forgot the part where it outputs the result to the user when I rewrote the script >.<

preguntado el 08 de noviembre de 11 a las 16:11

Can you post the javascript wich shows the alert? -

@George It's just in the source of the distrokiezer.htm file. But here's the Javascript seperately anyway: pastebin.com/ikLPpBRE -

2 Respuestas

The problem is that the warnings only appear when a field is left empty, because the first two fields are text input when left empty there value is nothing like your code checks for:

if ((stop == false) && (document.getElementById('q1a').value == ''))

However when your select boxes aren't modified their default value is the value of the first option causing the javascript not to trigger.

Put something like this as the first option in all your select forms:

<option value="" selected="selected">Please select an option</option>

This ensures that the default value is nothing. Just be sure to stop people (like you are already) using that option as it most likely means nothing.

Further to my comment this is why currently no alert is being recieved when two checkboxes are being filled.

Is the entire script....

    var distros = new Array('ubuntu', 'ubuntu-lts', 'mint', 'mintdebian', 'mintlxde', 'mintdebianxfce', 'elementary', 'debian-stable', 'debian-unstable', 'debian-testing', 'fedora', 'fedorakde', 'fedoralxde', 'fedoraxfce', 'centos', 'archlinux', 'puppylinux', 'lucidpuppy', 'crunchbang', 'dsl', 'tinycore', 'pinguyos', /*'gentoo',*/ 'mandriva', 'mageia', 'lubuntu', 'xubuntu', 'kubuntu', 'chakra', 'slackware', 'zorin', 'bodhi', 'vector', 'antix', 'opensuse');

    function zoekDistro()
    {
        try
        {
            var stop = false;

            //q1a
            var q1a = parseInt(document.getElementById('q1a').value);
            if ((stop == false) && (document.getElementById('q1a').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 1a!');
                stop = true;
            }
            if (stop == false)
            {
                if (q1a < 1600)
                {
                    dumpDistro('pinguyos');
                    dumpDistro('mintkde');
                    dumpDistro('kubuntu');
                }
                if (q1a < 1400)
                {
                    dumpDistro('mint');
                    dumpDistro('fedorakde');
                    dumpDistro('fedora');
                    dumpDistro('pinguyos');
                    dumpDistro('chakra');
                    dumpDistro('ubuntu');
                    dumpDistro('mandriva');
                    dumpDistro('opensuse');
                }
                if (q1a < 1200)
                {
                    dumpDistro('ubuntu-lts');
                }
                if (q1a < 1000)
                {
                    dumpDistro('mintdebian');
                    dumpDistro('debian-stable');
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('mageia');
                    dumpDistro('xubuntu');
                    dumpDistro('crunchbang');
                }
                if (q1a < 800)
                {
                    dumpDistro('mintlxde');
                    dumpDistro('mintdebianxfce');
                    dumpDistro('elementary');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('centos');
                    dumpDistro('archbang');
                    dumpDistro('zorin');
                }
                if (q1a < 600)
                {
                    dumpDistro('lubuntu');
                    dumpDistro('slackware');
                    dumpDistro('archlinux');
                }
                if (q1a < 400)
                {
                    dumpDistro('vector');
                    dumpDistro('gentoo');
                }
                if (q1a < 300)
                {
                    dumpDistro('antix');
                    dumpDistro('lucidpuppy');
                    dumpDistro('bodhi');
                }
                if (q1a < 200)
                {
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                }
                if (q1a < 100)
                {
                    dumpDistro('tinycore');
                }
            }
            //q2a
            var q1b = parseInt(document.getElementById('q1b').value);
            if ((stop == false) && (document.getElementById('q1b').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 1b!');
                stop = true;
            }
            if (stop == false)
            {
                if (q1b < 2000)
                {
                    dumpDistro('pinguyos');
                }
                if (q1b < 1600)
                {
                    dumpDistro('kubuntu');
                    dumpDistro('ubuntu');
                }
                if (q1b < 1400)
                {
                    dumpDistro('fedora');
                    dumpDistro('fedorakde');
                }
                if (q1b < 1000)
                {
                    dumpDistro('ubuntu-lts');
                    dumpDistro('mint');
                    dumpDistro('debian-unstable');
                    dumpDistro('mandriva');
                    dumpDistro('mageia');
                    dumpDistro('xubuntu');
                    dumpDistro('chakra');
                    dumpDistro('opensuse');
                }
                if (q1b < 800)
                {
                    dumpDistro('mintlxde');
                    dumpDistro('lubuntu');
                }
                if (q1b < 700)
                {
                    dumpDistro('elementary');
                }
                if (q1b < 512)
                {
                    dumpDistro('mintdebian');
                    dumpDistro('debian-testing');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('crunchbang');
                }
                if (q1b < 400)
                {
                    dumpDistro('mintdebianxfce');
                    dumpDistro('debian-stable');
                    dumpDistro('archbang');
                    dumpDistro('archlinux');
                    dumpDistro('centos');
                    dumpDistro('zorin');
                    dumpDistro('vector');
                }
                if (q1b < 256)
                {
                    dumpDistro('gentoo');
                    dumpDistro('slackware');
                }
                if (q1b < 128)
                {
                    dumpDistro('bodhi');
                    dumpDistro('puppylinux');
                }
                if (q1b < 64)
                {
                    dumpDistro('lucidpuppy');
                    dumpDistro('antix');
                }
                if (q1b < 48)
                {
                    dumpDistro('tinycore');
                }
            }
            //q2a
            var q2a = document.getElementById('q2a').value;
            if ((stop == false) && (q2a == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 2a!');
                stop = true;
            }
            if (stop == false)
            {
                if (q2a === 'nee')
                {
                    dumpDistro('debian-stable');
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                    dumpDistro('tinycore');
                    dumpDistro('gentoo');
                    dumpDistro('chakra');
                    dumpDistro('slackware');
                    dumpDistro('vector');
                    dumpDistro('mandriva');
                    dumpDistro('archbang');
                    dumpDistro('antix');
                }
            }
            //q2b
            var q2b = document.getElementById('q2b').value;
            if ((stop == false) && (q2b == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 2b!');
                stop = true;
            }
            if (stop == false)
            {
                if (q2b === 'ja')
                {
                    dumpDistro('debian-unstable');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('archbang');
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                    dumpDistro('tinycore');
                    dumpDistro('gentoo');
                    dumpDistro('vector');
                }
            }
            //q2c
            var q2c = parseInt(document.getElementById('q2c').value);
            if ((stop == false) && (document.getElementById('q2c').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 2c!');
                stop = true;
            }
            if (stop == false)
            {
                if (q2c < 8)
                {
                    dumpDistro('gentoo');
                    dumpDistro('archlinux');
                }
                if (q2c < 7)
                {
                    dumpDistro('tinycore');
                }
                if (q2c < 6)
                {
                    dumpDistro('debian-unstable');
                    dumpDistro('centos');
                    dumpDistro('archbang');
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                    dumpDistro('chakra');
                    dumpDistro('slackware');
                    dumpDistro('vector');
                    dumpDistro('antix');
                    dumpDistro('mandriva');
                }
                if (q2c < 5)
                {
                    dumpDistro('debian-testing');
                }
                if (q2c < 4)
                {
                    dumpDistro('crunchbang');
                }
                if (q2c < 3)
                {
                    dumpDistro('mintdebian');
                    dumpDistro('mintdebianxfce');
                    dumpDistro('debian-stable');
                    dumpDistro('fedora');
                    dumpDistro('fedorakde');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('mandriva');
                }
            }
            //q3a
            var q3a = parseInt(document.getElementById('q3a').value);
            if ((stop == false) && (document.getElementById('q3a').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 3a!');
                stop = true;
            }
            if (stop == false)
            {
                if (q3a == 4)
                {
                    dumpDistro('ubuntu');
                    dumpDistro('ubuntu-lts');
                    dumpDistro('elementary');
                    dumpDistro('fedorakde');
                    dumpDistro('opensuse');
                    dumpDistro('mageia');
                    dumpDistro('mint');
                    dumpDistro('mintlxde');
                    dumpDistro('lubuntu');
                    dumpDistro('kubuntu');
                    dumpDistro('xubuntu');
                }
                if (q3a == 3)
                {
                    dumpDistro('elementary');
                }
                else if (q3a == 1)
                {
                    dumpDistro('debian-unstable');
                    dumpDistro('debian-testing');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                    dumpDistro('gentoo');
                    dumpDistro('mandriva');
                    dumpDistro('vector');
                    dumpDistro('antix');                
                }
            }
            //q3b
            var q3b = document.getElementById('q3b').value;
            if ((stop == false) && (q3b == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 3b!');
                stop = true;
            }
            if (stop == false)
            {
                if (q3b == 'ja')
                {
                    dumpDistro('ubuntu');
                    dumpDistro('ubuntu-lts');
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('debian-stable');
                    dumpDistro('elementary');
                    dumpDistro('fedora');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('crunchbang');
                    dumpDistro('archbang');
                    dumpDistro('pinguyos');
                    dumpDistro('gentoo');
                    dumpDistro('mandriva');
                    dumpDistro('xubuntu');
                }
                if (q3b == 'neevoorwaarde')
                {
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('debian-stable');
                    dumpDistro('fedora');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('pinguyos');
                    dumpDistro('gentoo');
                    dumpDistro('mandriva');
                    dumpDistro('xubuntu');
                }
                if (q3b == 'neemacos')
                {
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('debian-stable');
                    dumpDistro('fedora');
                    dumpDistro('fedoralxde');
                    dumpDistro('fedoraxfce');
                    dumpDistro('centos');
                    dumpDistro('archlinux');
                    dumpDistro('crunchbang');
                    dumpDistro('archbang');
                    dumpDistro('gentoo');
                    dumpDistro('mandriva');
                    dumpDistro('mageia');
                    dumpDistro('xubuntu');
                    dumpDistro('mint');
                    dumpDistro('mintdebian');
                    dumpDistro('mintlxde');
                    dumpDistro('mintdebianxfce');
                    dumpDistro('fedorakde');
                    dumpDistro('puppylinux');
                    dumpDistro('lucidpuppy');
                    dumpDistro('dsl');
                    dumpDistro('lubuntu');
                    dumpDistro('kubuntu');
                    dumpDistro('chakra');
                    dumpDistro('slackware');
                    dumpDistro('zorin');
                    dumpDistro('vector');
                    dumpDistro('antix');
                    dumpDistro('opensuse');
                }
            }
            //q3c
            var q3c = document.getElementById('q3c').value;
            if ((stop == false) && (q3c == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 3c!');
                stop = true;
            }
            if (stop == false)
            {
                if (q3c == 'ja')
                {
                    dumpDistro('debian-stable');
                    dumpDistro('debian-testing');
                    dumpDistro('debian-unstable');
                    dumpDistro('centos');
                    dumpDistro('gentoo');
                }
            }
            //q3d
            var q3d = document.getElementById('q3d').value;
            if ((stop == false) && (q3d == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 3d!');
                stop = true;
            }
            if (stop == false)
            {
                if (q3d == 'stabiel')
                {
                    dumpDistro('ubuntu');
                    dumpDistro('mintdebian');
                    dumpDistro('mintdebianxfce');
                    dumpDistro('debian-unstable');
                    dumpDistro('debian-testing');
                    dumpDistro('fedora');
                    dumpDistro('puppylinux');
                    dumpDistro('dsl');
                    dumpDistro('pinguyos');
                    dumpDistro('mandriva');
                    dumpDistro('kubuntu');
                    dumpDistro('chakra');
                    dumpDistro('slackware');
                    dumpDistro('opensuse');
                }
                else if (q3d == 'test')
                {
                    dumpDistro('debian-stable');
                    dumpDistro('debian-unstable');
                }
                else if (q3d == 'experimenteel')
                {
                    dumpDistro('debian-stable');
                }
            }
            if (distros.length > 6)
            {
                if ((q1a > 1000) && (q1b > 1000))
                {
                    dumpDistro('mintlxde');
                    dumpDistro('mintdebianxfce');
                    if (distros.length > 3) { dumpDistro('fedoralxde'); }
                    if (distros.length > 3) { dumpDistro('fedoraxfce'); }
                    if (distros.length > 3) { dumpDistro('mintdebianxfce'); }
                    if (distros.length > 3) { dumpDistro('lucidpuppy'); }
                    if (distros.length > 3) { dumpDistro('crunchbang'); }
                    if (distros.length > 3) { dumpDistro('dsl'); }
                    if (distros.length > 3) { dumpDistro('tinycore'); }
                    if (distros.length > 3) { dumpDistro('bodhi'); }
                    if (distros.length > 3) { dumpDistro('antix'); }
                    if (distros.length > 4)
                    {
                        if (q3b != 'ja')
                        {
                            dumpDistro('zorin');
                        }
                    }
                }
            }
        }
        catch (error)
        {
            alert(error);
        }
    }
    function dumpDistro(distro)
    {
        for (var i = 0; i < distros.length; i++ )
        { 
            if (distros[i] == distro)
            {
                distros.splice(i,1); 
            }
        } 
    }
</script>

var stop = false; so let's remember that.

if ((stop == false) && (document.getElementById('q1a').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 1a!');
                stop = true;
            }

Doesn't fire when input has content.

if ((stop == false) && (document.getElementById('q1b').value == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 1b!');
                stop = true;
            }

Doesn't fire when input has content.

  if ((stop == false) && (q2a == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 2a!');
                stop = true;
            }

Doesn't fire when input has content.

 if ((stop == false) && (q2b == ''))
            {
                alert('Geen antwoord opgegeven bij vraag 2b!');
                stop = true;
            }

Doesn't fire when input has content ... and so on...

Can you post the specific alert you think should fire even when no values are empty?

respondido 10 nov., 11:19

Thanks for the tip, but that's not the issue :p The problem is that it also does not show the alert on the end of the zoekDistro() function. - RobinJ

@RobinJ The error isn't happening because there is nothing wrong with your code, the calls are made and no built in errors are triggered to handle so your try catch succeeds. Failing an if doesn't trigger an error. My code definitely solves the problem of no alert appearing as your alert in your if now functions. - George Reith

I'm pretty sure that, either you understand the question wrong, or I understand your answer wrong... Anyway, I did what you told me, to no effect. If everything is filled in, and one clicks the submit button, nothing happens. By the way, I wanted the first answer to be selected by default. The check I they were empty in the javascript code was just done in case I every changed my mind about that. - RobinJ

@RobinJ I misunderstood what you wanted, but what are you expecting to happen? you haven't told it to do anything except distros.splice(i,1);... this won't make your page change. - George Reith

Take a good look at the script. No matter if the script fails or not, it should at least pop up an alert. - RobinJ

Having a quick look in the code I can see a submit button with an onclick handler which appears to be working.

However there is no FORM tag surrounding the fields and the submit button so the browser does not know what to submit and to where.

respondido 08 nov., 11:20

As I thought, this makes no difference as I'm just using the onClick event of the button. - RobinJ

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.