Set a limit on checkbox and when it reaches the limit submit button will show PHP & jQuery -
i want limit checkboxes , when reached limit submit button show, once submit button shows, , user disabled 1 of checkbox, submit button disabled.
here's code:
<?php if (isset($_post['lol'])){ foreach ($_post["lol"] $pastry) { if ($pastry=="cake") { //make sure option chose part of options :p echo "<li>cake</li>"; } if ($pastry=="pie") { echo "<li>pie</li>"; } if ($pastry=="cupcakes") { echo "<li>cupcakes</li>"; } if ($pastry=="brownies") { echo "<li>brownies</li>"; } } } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script type="text/javascript"> /*********************************************** * limit number of checked checkboxes script- javascript kit (www.javascriptkit.com) * notice must stay intact usage * visit javascript kit @ http://www.javascriptkit.com/ script , 100s more ***********************************************/ function checkboxlimit(checkgroup, limit){ var checkgroup=checkgroup var limit=limit (var i=0; i<checkgroup.length; i++){ checkgroup[i].onclick=function(){ var checkedcount=0 (var i=0; i<checkgroup.length; i++) checkedcount+=(checkgroup[i].checked)? 1 : 0 if (checkedcount>limit){ alert("you can select maximum of "+limit+" checkboxes") this.checked=false } } } } </script> </head> <body> <form action="test.php" id="poll" name="poll" method="post"> <input type="checkbox" id='lol' name="lol[]" value="cake"> cake :d <br> <input type="checkbox" id='lol' name="lol[]" value="pie"> pie <br> <input type="checkbox" id='lol' name="lol[]" value="cupcakes"> cupcakes ^o^ <br> <input type="checkbox" id='lol' name="lol[]" value="brownies"> brownies :d <br> <input type="submit" value="vote"> </form> <script type="text/javascript"> //syntax: checkboxlimit(checkbox_reference, limit) checkboxlimit(document.forms.poll.lol, 2) </script> </body> </html>
var elems = $('#poll input[type="checkbox"]'), subm = $('#poll input[type="submit"]').hide(); elems.on('change', function () { var limit = 2, _check = elems.filter(':checked').length; if (_check == limit) { subm.show(); }else if (_check > limit) { alert("you can select maximum of " + limit + " checkboxes") this.checked = false; }else{ subm.hide(); } });
Comments
Post a Comment