0

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;
    }
});
Austin
  • 1,619
  • 7
  • 25
  • 51

3 Answers3

1

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;
    }
});
Kyle Goode
  • 1,108
  • 11
  • 15
0

'alert' is a function, so you have to call it setting the arguments within parentheses as follows.

alert("error message");

and instead exit you have to put 'return;'

0

@Austin, your code is correct But the alert syntax is incorrect!!!

Correct syntax

alert ("I have an error...!");

Also you can use console.log + development tools of your browser (F12) to monitor your messages and objects values.

Correct code + console.log statement

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

Result enter image description here

I used Firefox + Firebug in my answer ;)

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98