-1

I am trying to use into an if statement if any of the options are selected...like:

<script>
    if(any_selected){
      if(option){
        //do somthing
      }
    }
    else{
      //do somthing else
    } </script

This is the html code:

    <div class="form-group">
    <label for="event-c" id="event-c" class="col-sm-4 control-label"><button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">button</button></label>
    <div class="col-sm-7"> 
    <div id="demo" class="collapse">
    <form>
        <c:forEach items="${getNamesForLegend}" var="members">
            <div class="radio">
                <div class="panel-body" id="radio-c"><label><input type="radio" name="optradio" value=""><c:out value="${members.name}"/> <c:out value="${members.lastname}"/> 
        <input type="hidden" name="a"<c:out value="${members.mySubordinate}"/>></div></label>
             </div>
        </c:forEach>

    </form>

</div>
</div>   
</div>
Alexa
  • 61
  • 5

2 Answers2

0

Try like below:

if($('input:[name=optradio]:checked').length)
{
//selected
}
else
{
//not selected
}

If you want to check particular group of radio button use above.Or else try $('input:checkbox:checked').length

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
0

Calculate :checked count.

$(document).ready(function () {
  $('input').change(function () {
    if ($('input:checked').length) {
      $('span').css({'background-color': 'green'});
    } else {
      $('span').css({'background-color': 'red'});
    }
  });
});
input {
  margin-left: 10px;
  margin-bottom: 10px;
}

span {
  width: 20px;
  height: 20px;
  background-color: red;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<hr/>
<span></span>

Keep in mind that your #radio-c is in loop and will have non-unique ID per page.

Justinas
  • 41,402
  • 5
  • 66
  • 96