Last active
December 22, 2015 00:09
-
-
Save zeuxisoo/6387153 to your computer and use it in GitHub Desktop.
Checkbox demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $check = isset($_POST['check']) ? $_POST['check'] : array(); | |
| $types = array( | |
| 1 => '報紙', | |
| 2 => '雜誌', | |
| 3 => '其他' | |
| ); | |
| echo '<h3>',"Checked:",'</h3>'; | |
| foreach($check as $value) { | |
| if (array_key_exists($value, $types) === true) { | |
| echo "<p>",$types[$value],"</p>"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
| <script> | |
| (function($) { | |
| $(function() { | |
| $("form").on('submit', function() { | |
| if ($('input[name="check[]"]:checked').length <= 0) { | |
| alert("Please check at least one item"); | |
| return false; | |
| } | |
| return true; | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <form action="checkbox.php" method="post"> | |
| <input type="checkbox" name="check[]" value="雜誌" /> 雜誌 | |
| <input type="checkbox" name="check[]" value="報紙" /> 報紙 | |
| <input type="checkbox" name="check[]" value="其他" /> 其他 | |
| <input type="submit" value="submit"> | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
| <script> | |
| (function($) { | |
| $(function() { | |
| $("form").on('submit', function() { | |
| if ($('input.must_checked').is(':checked') === false) { | |
| alert("Please check at least one item"); | |
| return false; | |
| } | |
| return true; | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <form action="checkbox.php" method="post"> | |
| <input type="checkbox" name="check[1]" value="雜誌" class="must_checked" /> 雜誌 | |
| <input type="checkbox" name="check[2]" value="報紙" class="must_checked" /> 報紙 | |
| <input type="checkbox" name="check[3]" value="其他" class="must_checked" /> 其他 | |
| <input type="submit" value="submit"> | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
| <script> | |
| (function($) { | |
| $(function() { | |
| $("form").on('submit', function() { | |
| var groupNames = []; | |
| $(".must_checked").each(function() { | |
| var group = $(this).attr('rel'); | |
| if ($.inArray(group, groupNames) === -1) { | |
| groupNames.push(group); | |
| } | |
| }); | |
| for(var i=0; i<groupNames.length; i++) { | |
| var groupName = groupNames[i]; | |
| if ($("input[rel=" + groupName + "]").is(":checked") === false) { | |
| alert(groupName + " must as least one checked"); | |
| return false; | |
| } | |
| } | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <form action="checkbox.php" method="post"> | |
| <input type="checkbox" name="check[1]" value="雜誌" rel="group1" class="must_checked" /> 雜誌 | |
| <input type="checkbox" name="check[2]" value="報紙" rel="group1" class="must_checked" /> 報紙 | |
| <input type="checkbox" name="check[3]" value="其他" rel="group1" class="must_checked" /> 其他 | |
| <input type="checkbox" name="check[1]" value="雜誌" rel="group2" class="must_checked" /> 雜誌 | |
| <input type="checkbox" name="check[2]" value="報紙" rel="group2" class="must_checked" /> 報紙 | |
| <input type="checkbox" name="check[3]" value="其他" rel="group2" class="must_checked" /> 其他 | |
| <input type="submit" value="submit"> | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <form action="checkbox.php" method="post"> | |
| <input type="checkbox" name="check[]" value="1"> 報紙 | |
| <input type="checkbox" name="check[]" value="2"> 雜誌 | |
| <input type="checkbox" name="check[]" value="3"> 其他 | |
| <input type="submit" value="submit"> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment