0

I have 3 groups of radiobuttons, all of them properly named. On submit() I want to check if user selected option from each of groups.

I do that that way:

 $('.form').submit(function(){
      var selectedUser = $('input[name="selectedUser"]').val();
      var searchType = $('input[name="shareType"]').val();
      var selectedSearch = $('input[name="selectedSearch"]').val();
      console.log(selectedUser, searchType, selectedSearch);

      return false; //for testing purposes
    });

The problem is that each of three variables return not null values even if none of radiobuttons is selected.

Fact that might be important: each of groups contains more than 1 radiobutton, each radiobutton has distinct value.

ex3v
  • 3,518
  • 4
  • 33
  • 55
  • 1
    duplicate of http://stackoverflow.com/questions/4138859/jquery-how-to-get-selected-radio-button-value – Ram Singh Oct 28 '13 at 13:04

1 Answers1

2

You need to test if the radio is selected, you can do this using the :checked pseudo-selector:

  var selectedUser = $('input[name="selectedUser"]:checked').val();
  var searchType = $('input[name="shareType"]:checked').val();
  var selectedSearch = $('input[name="selectedSearch"]:checked').val();
  if(selectedUser || searchType || selectedSearch) {
      // At least one selected
  }
  else {
      // None selected
      return false;
  }

Here is a fiddle which demonstrates.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176