views:

1523

answers:

4

I have two radio buttons like so

<input type="radio" name="rush" value="yes" />
<input type="radio" name="rush" value="no" checked="checked" />

What I want to do is when the form submit button is clicked, check if 'yes' is selected.

If it is, show a javascript confirm dialog

I've gotten this far, but am stuck, any help would be appreciated.

$("form.shipping").submit(function() {

 if($("input[name='rush']".attr("value") === "yes")
 {
  if (confirm("Rush orders are subject to an upcharge. (Just wanted to make sure you read the note). Is this ok?"))
  {
   return true;
  }
  else
  {
   return false;
  }
 }

});
+5  A: 

You are binding something to the click event of a button. You probably want to bind it to the submit event of the form. Besides that, you misspelled input.sumbit-button in your selector.

EDIT: Here is a working version of what you want. Your errors were:

  • You were probably not wrapping your code around document.ready. This is important if your script is included at the top of the page, as anything you write that is not wrapped in this event will execute before the DOM elements are created. So you would be binding events to nothing. The document.ready event takes care of that by firing as soon as the DOM is fully loaded.
  • You had a missing closing ) right before .attr. You should probably look into tools like FireBug to debug your javascript, as it is trivial to find these kind of things with it.
  • You were checking the value of the radio button, but it was always evaluating to true because you need to filter it out to only get the :checked one.

So, the working code:

$(document).ready(function() {
    $("form.shipping").submit(function() {
        if($("input[name='rush']:checked").attr("value") === "yes") {
            return confirm("Rush orders are subject to an upcharge.
            (Just wanted to make sure you read the note). Is this ok?");
        }
    });
});
Paolo Bergantino
That fixed it! You rock.
mjr
I did have the document ready but neglected to post it :-( my bad
mjr
Ah. Roger. I'm going to leave the warning in there anyways as a warning to anyone that reads this later on. Glad it worked.
Paolo Bergantino
A: 

your if else block should be replaced with:

return confirm("You want to rush this. Is this ok?");

(not that has anything to do with your misspelling problem)

edit, better yet do this:

$("input.sumbit-button").click(function() {
        return ($("input[name='rush']".attr('value') === "yes" && confirm("You want to rush this. Is this ok?"));
});
mkoryak
I agree with the first change, but the 2nd one is making it shorter at the cost of being readable.
Paolo Bergantino
this would be a nasty dirty useless hack. I very much agree with you as far as the return confirm(); goes, but there's no need to bang everything into the same line of code.
Peter Perháč
how would I do the return without the if else?
mjr
i disagree, it not any less readable. in js you will often see stuff like var a = something || somethingelse || 42;is that any less readable then making a bunch of if statements to accomplish the same thing?plus, doing it my way requires 1 return, doing it your way requires 2 returns.
mkoryak
it's a matter of preference. i'm not saying I have not written js code of that sort myself (maybe I have... although i doubt it). My point is, it doesn't matter much how many expressions you evaluate in an if statement, but spawning confirmation boxes just does not belong between the () after if :)
Peter Perháč
better:return ( confirm("You want to rush this. Is this ok?") =)
mkoryak
A: 

Does it fail in multiple browsers? I've seen reports of some odd bugs in IE7 where confirm() didn't execute in an onclick or onsubmit event handler.

Jeff Mc
only tested in firefox at this point
mjr
A: 

your selector "input[name='rush']" will return both inputs, you need "input[name='rush']:checked" as Paolo Bergantino suggested

Jeff Mc