trouble with confirmationboxes when submitting a form

37 views Asked by At

I'm trying to compare some values of select fields. When submitting my form I want to check whether the values are equal.

Well ... I've got two problems here.

  1. The values aren't compared the first time I submit the form. Example: I select option3 for id='idx' and id='idy'. In my case option3 is equal to option3. When I submit the form there should be a confirm box because the values are equal. No confirmationbox appears. I call the site again with the set values and submit the form; the confirmationbox appears. I chose option 1 for id='idx'. This would mean that the values of id='idx' and id='idy' are not equal. But now the box appears again.

What I want is that when I submit the form the values should be compared. In case they're equal a confirmationbox appears.

2.Somehow my code only checks for one condition. But it is possible that the two cases in my code can be true. How would I solve it?

This is my code:

  
var x = $('#idx option:selected').val();
var y = $('#idy option:selected').val();
var z = $('#idz option:selected').val();

$('#myform').submit(function() {
    if (x === y ) {
        if (confirm('text') == true){
            return true;
        } else {
            return false;
        }
    }
    if (x === z ) {
            if (confirm('text') == true){
            return true;
        } else {
            return false;
        }
    }
});

If there are any questions, please ask. Thank you for your help.

1

There are 1 answers

2
Detect On BEST ANSWER

First, you should put your $('...').val() lines inside the submit function, or else they will get the value of the select boxes when the page loads.

Second, once you return true or false, it will exit out of that submit function, so you should return false when it fails, or else continue until the end of your conditionals, then have one return true at the end.