jQuery: Add values of checkboxes to input text field

后端 未结 4 694
梦如初夏
梦如初夏 2021-01-16 13:17

I\'m trying to add the values of any checked checkbox to an input text field. Here\'s my fiddle: http://jsfiddle.net/Lf6ky/

4条回答
  •  不思量自难忘°
    2021-01-16 14:07

    On click of a checkbox, loop through the checked inputs, append to a string then assign that to your text box:

    $(document).ready(function() {
      $("input:checkbox").click(function() {
        var output = "";
        $("input:checked").each(function() {
          output += $(this).val() + " ";
        });
        $("#field_results").val(output.trim());
      });
    });
    
    
    1
    2
    3

提交回复
热议问题