javascript - Using jQuery checking if multiple checkboxes are checked? - Stack Overflow

Hello I'm wondering if it's possible to use jQuery to check if checkboxes are checked in a sm

Hello I'm wondering if it's possible to use jQuery to check if checkboxes are checked in a smart way, let's say that we use a switch case with a bunch of input fields, as an example I got 5 checkboxes. Company, name, username, age, password. And if you check Company, I can run my code.

I want to do this in the smartest way as possible in jQuery, let's say I want something like this in pseudo

switch(checkboxarray?) {

    case Company: 
    document.write("Company: INPUT FIELD");
    break;

}

I've looked into this but I've only found solutions that provide a function for one checkbox

<script src="//ajax.googleapis/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">  
$(document).ready(function() {
    $('#messageVisibilityCheckbox').change(
        function () {
            if ($('#messageVisibilityCheckbox').is(':checked')) {
                alert('is checked'); //document.write("Company: INPUT FIELD);
            }  
            else {

            }
    });
});
</script>

and the HTML:

sinput type="checkbox" id="messageVisibilityCheckbox" name="messageVisibilityCheckbox"/>

so instead of creating a bunch of functions, I want to create one function with a switch case or something equal were I can just add a few lines for each checkbox. The reason why I need this is because I'll have like 30 checkboxes and half of them requires an extra input field, any help or source is highly appreciated! Thanks!

Hello I'm wondering if it's possible to use jQuery to check if checkboxes are checked in a smart way, let's say that we use a switch case with a bunch of input fields, as an example I got 5 checkboxes. Company, name, username, age, password. And if you check Company, I can run my code.

I want to do this in the smartest way as possible in jQuery, let's say I want something like this in pseudo

switch(checkboxarray?) {

    case Company: 
    document.write("Company: INPUT FIELD");
    break;

}

I've looked into this but I've only found solutions that provide a function for one checkbox

<script src="//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">  
$(document).ready(function() {
    $('#messageVisibilityCheckbox').change(
        function () {
            if ($('#messageVisibilityCheckbox').is(':checked')) {
                alert('is checked'); //document.write("Company: INPUT FIELD);
            }  
            else {

            }
    });
});
</script>

and the HTML:

sinput type="checkbox" id="messageVisibilityCheckbox" name="messageVisibilityCheckbox"/>

so instead of creating a bunch of functions, I want to create one function with a switch case or something equal were I can just add a few lines for each checkbox. The reason why I need this is because I'll have like 30 checkboxes and half of them requires an extra input field, any help or source is highly appreciated! Thanks!

Share Improve this question edited Oct 7, 2014 at 18:51 Denniz asked Oct 7, 2014 at 18:31 DennizDenniz 1671 gold badge3 silver badges12 bronze badges 3
  • 2 Your question is unclear and your pseudo-code is not helping to clear anything up. Before you do anything in "the smartest way possible", try to do it at all. And you don't really need jQuery for this, you should be able to solve it without. – Tomalak Commented Oct 7, 2014 at 18:35
  • 1 Thank you for your opinion, I've fixed it now so it should be easier to understand! – Denniz Commented Oct 7, 2014 at 18:56
  • Definitely much better! – Tomalak Commented Oct 7, 2014 at 18:59
Add a ment  | 

1 Answer 1

Reset to default 4

You could group multiple individual HTML elements under the same parent and use jQuery's event delegation.

Example

$(function() {
    $('#allCheckboxes').on("change", ":checkbox", function () {
        if (this.checked) {
            console.log(this.id + ' is checked');
        } else {
            console.log(this.id + ' is unchecked');
        }
    });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id="allCheckboxes">
  <input type="checkbox" id="chkBox1" name="Box1" />
  <input type="checkbox" id="chkBox2" name="Box2" />
  <input type="checkbox" id="chkBox3" name="Box3" />
  <input type="checkbox" id="chkBox4" name="Box4" />
  <input type="checkbox" id="chkBox5" name="Box5" />
</div>

Hint: Don't use document.write(). Ever. Just forget it exists, there is no reason that would justify using this function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744701242a4588790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信