Search code examples
jqueryhtmlif-statementjquery-mobile-flipswitch

jQuery multi part if statement not working


I have a jQuery Mobile form I am working on where when the user tries to submit the form I would like to check the settings of 2 inputs. The first input is a jquery mobile flipswitch and the second is a checkbox.

If my flipswitch value is 1 and my checkbox is checked I would like to throw and error. The jQuery code below is not working for me. Pretty sure I'm on the right track but I don't know what I am doing wrong here. Anyone have any ideas?

my HTML

<select name="flip-min" id="flip-min" data-role="slider">
    <option id="filled" value="1">Filled</option>
    <option id="notFilled" value="0">Un-Filled</option>
</select>

<input type="checkbox" name="unable" id="unable"/>
<label for="unable">label text</label>

<input type="button" name="submit" value="submit">

my jQuery

$("input[name=submit]").on("click", function(){
    if($('#unable').is(':checked') && $('#flip-min').val() == 1){
        alert "error message";
        exit;
    }
});

Solution

  • alright, there appears to be a few things wrong with your code. First, there is no parenthesis around your alert function. This will definitely cause an error. Secondly, you have "exit;" at the end of the if statement when in reality, you want to use "return;". Here is what your code should look like:

    $("input[name=submit]").on("click", function(){
        if($('#unable').is(':checked') && $('#flip-min').val() == 1){
            alert("error message");
            return;
        }
    });