Tuesday, 23 December 2014

jQuery - if a radio button not selected, cannot submit the form

<!DOCTYPE html>
<html>
<head></head>
<body>
  <form action="/" method="post">    
    <div id="customSize">something</div>
    <input type="radio" name="EmbedSizeID" value="1" onClick="OptionSelected()" />
    <input type="radio" name="EmbedSizeID" value="2" onClick="OptionSelected()" />
    <input type="submit" value="Save" id="submitButton" />
  </form>
  <script src="http://code.jquery.com/jquery-1.7.1.js"></script>
  <script type="text/javascript">
    function OptionSelected() {
                document.getElementById("customSize").style.display = 'none';
                var selected = $('input[name="EmbedSizeID"]:checked').val();
                if (selected == "4") {
                    document.getElementById("customSize").style.display = 'block';
                }
            }

            $(document).ready(function () {
                $("#submitButton").click(function (e) {
                    if (!$('input[name="EmbedSizeID"]').is(':checked')) {
                        return false;
                    }
                    else {
                        return true;
                    }
                });
            });
  </script>
</body>
</html>

No comments:

Post a Comment