简体   繁体   English

从checkbox jQuery中获取动态值

[英]Get dynamically value from checkbox jQuery

I need help with get value from checkbox with jQuery. 我需要帮助从jQuery获取复选框的值。

 $( document ).ready( function() { var value_array = []; $( document ).on( 'change', '.radio-group input', function(e) { var $this = $( this ), value = $this.val(); value_array.push( value ); console.log( $.unique( value_array ) ); $( '#out' ).html( $.unique( value_array ).join() ) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="radio-group"> <label> <input type="checkbox" name="cat_1" value="90" /> Category 1 </label> <label> <input type="checkbox" name="cat_2" value="43" /> Category 2 </label> </div> <div id="out"> </div> 

  1. If category 1 checked, getting value (correct). 如果选中类别1,则获取值(正确)。
  2. If category 2 checked, getting value (correct). 如果选中类别2,则获取值(正确)。
  3. If category 1 un-checked, getting value again (false, i don't want it). 如果未选中类别1,则再次获取值(错误,我不想要它)。
  4. If category 2 un-checked, getting value again (false, i don't want it). 如果未选中类别2,则再次获取值(错误,我不想要它)。

I want like this: 我想这样:

  1. If category 1 un-checked, remove the value from output array. 如果未选中类别1,则从输出数组中删除该值。

  2. If category 2 un-checked, remove the value from output array. 如果未选中类别2,则从输出数组中删除该值。

Check if checkbox is checked, add value into array if it is, remove if it's not. 检查是否选中了复选框,如果是,则将值添加到数组中,如果不是则删除。

 $(document).ready(function() { var value_array = []; $(document).on('change', '.radio-group input', function(e) { var $this = $(this), value = $this.val(); if ($this.prop('checked')) value_array.push(value); else value_array.splice(value_array.indexOf(value), 1); console.log($.unique(value_array)); $('#out').html($.unique(value_array).join()) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="radio-group"> <label> <input type="checkbox" name="cat_1" value="90" /> Category 1 </label> <label> <input type="checkbox" name="cat_2" value="43" /> Category 2 </label> </div> <div id="out"> </div> 

You can just fetch an array of all checked values at once: 您可以一次获取所有已检查值的数组:

$( document ).ready( function() {
  var value_array = [];

  $( document ).on( 'change', '.radio-group input', function(e) {
    value_array = $('.radio-group input:checked').map(function() {
                 return $(this).val();
              }).get();

    console.log( $.unique( value_array ) );
    $( '#out' ).html( $.unique( value_array ).join() )
  });
});

JSFiddle 的jsfiddle

You don't have to declare an array to begin with (which will pollute your namespace anyway). 您不必声明一个数组开始(无论如何都会污染您的命名空间)。 You can simply select for all the checkboxes, use .filter() to keep those that are checked, and the use .map() to return their values, all done within the callback of the onchange event listener: 您可以简单地选择所有复选框,使用.filter()来保留那些被检查的复选框,并使用.map()返回它们的值,所有这些都在onchange事件监听器的回调中完成:

// Get values of checked checkboxes
var value_array = $('.radio-group input').filter(':checked').map(function() {
  return this.value;
}).get();

console.log(value_array);

Note: Remember to chain .get() at the end of .map() , because it will return a jQuery object/collection and you have to convert it into an array. 注意:请记住在.map()末尾链接.get() ,因为它将返回一个jQuery对象/集合,您必须将其转换为数组。

See proof-of-concept example below: 请参阅下面的概念验证示例:

 $(document).ready(function() { $(document).on('change', '.radio-group input', function(e) { var $this = $(this), value = $this.val(); // Get values of checked checkboxes var value_array = $('.radio-group input').filter(':checked').map(function() { return this.value; }).get(); console.log(value_array); $('#out').html(value_array.join()) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="radio-group"> <label> <input type="checkbox" name="cat_1" value="90" /> Category 1 </label> <label> <input type="checkbox" name="cat_2" value="43" /> Category 2 </label> </div> <div id="out"> </div> 

First, you should understand that the "this" value refers to the object that owns the code. 首先,您应该了解“this”值是指拥有代码的对象。 In your case, the "this" in 在你的情况下,“这”在

     $( document ).on( 'change', '.radio-group input', function(e)

refers to the "document" itself and not the ".radio-group input". 指的是“文档”本身,而不是“.radio-group输入”。 Instead you should do the following so that the "this" refers to your checkbox. 相反,您应该执行以下操作,以便“this”指向您的复选框。

    var arr = [];
    $(".radio-group input").change(function(){

    var val = $(this).val();

    //push the value into the array if the checkbox is checked
   if($(this).prop("checked")==true)
   {
      arr.push(val);

   }

  //otherwise, remove the value from the array
   else{
  //fetch the index of the value in the array
   var index = arr.indexOf(val);

   //remove that value from the index
   arr.splice(index,1);
 }

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM