someoneb100 someoneb100 - 5 months ago 12
jQuery Question

JQuery form of radio input

I am trying to make a simple form of radio inputs where at most 1 can be selected:

<form id='forma' action='predmeti.php' method='GET'>
1:<input type='radio' name='godina' value='1'/>
2:<input type='radio' name='godina' value='2'/>
3:<input type='radio' name='godina' value='3'/>
<input type='button' id='ok' value='Izaberi godinu!'>
</form>


How do i allow form submit only if one is selected and prevent submit if nothing selected? The form is submited by clicking on the id='ok' button.

Answer

Updated based on comment. Please try this.

$(document).ready(function() {
       $('#ok').on('click', function(){
        if($("#forma input[type='radio']:checked").length == 1){
            $(this).closest("form").submit();
        }
       });
    });
Comments