javascript - Validate checkbox selections -
please check javascript code bellow. once click on "upload" getting "you didn't checked item" checked item same alert run. how can correct execute per item have selected?
<!doctype html> <html> <head> <title>test</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <form> <input type="checkbox" name="check_item" value="1">item 1<br/> <input type="checkbox" name="check_item" value="2">item 2<br/> <input type="checkbox" name="check_item" value="3">item 3<br/> <input type="button" class="uploadexternal" value="upload"/> </form> <script type="text/javascript"> $(document).on("click", ".uploadexternal", function (e) { var check_item = document.getelementsbyclassname("check_item"); if (check_item.checked) {//check if check_item empty alert("you have checked item"); } else { alert("you didn't checked item"); } }); </script> </body> </html>
the method document.getelementsbyclassname("check_item")
returns array of elements. need iterate on them. checked
attribute can determine if checkbox checked. result this:
var check_item = document.getelementsbyclassname("check_item"); var ischecked = false; (var = 0; < check_item.length; i++) { if (check_item[i].checked) { ischecked = true; break; } } if (ischecked) { ...
Comments
Post a Comment