How to retrieve checkboxes values in jQuery

前端 未结 15 2319
时光说笑
时光说笑 2020-11-22 10:11

How to use jQuery to get the checked checkboxes values, and put it into a textarea immediately?

Just like this code:


  
  &l         


        
15条回答
  •  再見小時候
    2020-11-22 11:08

    Here's one that works (see the example):

     function updateTextArea() {         
         var allVals = [];
         $('#c_b :checked').each(function() {
           allVals.push($(this).val());
         });
         $('#t').val(allVals);
      }
     $(function() {
       $('#c_b input').click(updateTextArea);
       updateTextArea();
     });
    

    Update

    Some number of months later another question was asked in regards to how to keep the above working if the ID changes. Well, the solution boils down to mapping the updateTextArea function into something generic that uses CSS classes, and to use the live function to monitor the DOM for those changes.

提交回复
热议问题