javascript - Appending Checkbox Value jQuery - Stack Overflow

I have a created a function to alert the user all the values they have checked from the checkboxes. For

I have a created a function to alert the user all the values they have checked from the checkboxes. For example if the user selects '1' and '2' then it should alert '1, 2' or if the user select '1' and '3' it should alert 1, 3 etc. However, it is not working. Any help would be appreciated :)

<form>
  <input class="1" type="checkbox" name="1" value="1" onclick="add()">1
  <input class="2" type="checkbox" name="2" value="2" onclick="add()">2
  <input class="3" type="checkbox" name="3" value="3" onclick="add()">3
  <input class="4" type="checkbox" name="4" value="4" onclick="add()">4
</form>

Here is a jsfiddle.

I have a created a function to alert the user all the values they have checked from the checkboxes. For example if the user selects '1' and '2' then it should alert '1, 2' or if the user select '1' and '3' it should alert 1, 3 etc. However, it is not working. Any help would be appreciated :)

<form>
  <input class="1" type="checkbox" name="1" value="1" onclick="add()">1
  <input class="2" type="checkbox" name="2" value="2" onclick="add()">2
  <input class="3" type="checkbox" name="3" value="3" onclick="add()">3
  <input class="4" type="checkbox" name="4" value="4" onclick="add()">4
</form>

Here is a jsfiddle.

Share Improve this question asked Jun 5, 2015 at 16:16 Ryan HawdonRyan Hawdon 3892 gold badges6 silver badges15 bronze badges 3
  • You should probably add some javascript to a javascript-tagged question. I know it's in the Fiddle, but you need to also include it in your post. – Tim Lewis Commented Jun 5, 2015 at 16:19
  • You're using an event listener inside a function that called on onClick. Or I'm missing here something or something went wrong? – Ofir Baruch Commented Jun 5, 2015 at 16:20
  • Sorry, the JSfiddle shouldn't have the click function. – Ryan Hawdon Commented Jun 5, 2015 at 16:22
Add a ment  | 

4 Answers 4

Reset to default 2

You could just do like below.

$(':checkbox').click(function() {
    alert($(':checkbox:checked').map(function() {
      return this.value;
    }).get());
});

The demo.

$('input:checkbox').on('change', function() {
    var chkd = $('input:checkbox:checked');
    if( chkd.length == 2 ) {
        var vals = chkd.map(function() {
            return this.value;
        })
        .get().join(', ');
        alert( vals );
    }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input class="1" type="checkbox" name="1" value="1"/>1
  <input class="2" type="checkbox" name="2" value="2"/>2
  <input class="3" type="checkbox" name="3" value="3"/>3
  <input class="4" type="checkbox" name="4" value="4"/>4
</form>

UPDATE

$(function() {
    $('#myButton').on('click', function() {
        var text = $('input[name=somename]').val();
        var chkd = $('input:checkbox:checked');
        var vals = chkd.map(function() {
            return this.value;
         })
        .get().join(', ');
        alert("Name: " + text + "\n" + "Checkbox: " + vals );
    });
});

DEMO

function add() {
  var checked = '';
  $(':checked').each(function() {
    checked += $(this).val() + ',';
  });
  console.log(checked);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input class="1" type="checkbox" name="1" value="1" onclick="add()">1
<input class="2" type="checkbox" name="2" value="2" onclick="add()">2
<input class="3" type="checkbox" name="3" value="3" onclick="add()">3
<input class="4" type="checkbox" name="4" value="4" onclick="add()">4

One more way is this:

$("input[type='checkbox']").change(function() {
  $(':checked').each(function() {
    alert($(this).val());
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="footboard" type="checkbox" name="1" value="1">1
<input class="drawers" type="checkbox" name="2" value="2">2
<input class="casters" type="checkbox" name="3" value="3">3
<input class="squeak" type="checkbox" name="4" value="4">4

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

相关推荐

  • javascript - Appending Checkbox Value jQuery - Stack Overflow

    I have a created a function to alert the user all the values they have checked from the checkboxes. For

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信