最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove the unchecked item from Array using jquery - Stack Overflow

programmeradmin3浏览0评论

I have this code to put it in array when user checks the check box.. but when user unchecks the check box how do I need to remove from the Array?

 $(function () {
   var listval = [];
        $('input[name="checkedRecords"]').bind('click', function () {
            if ($(this).is(':checked')) {
                listval.push($(this).val());
            }
            else {
                //How to remove the Unchecked item from array..
            }
        });
    });

Thanks in advance.

I have this code to put it in array when user checks the check box.. but when user unchecks the check box how do I need to remove from the Array?

 $(function () {
   var listval = [];
        $('input[name="checkedRecords"]').bind('click', function () {
            if ($(this).is(':checked')) {
                listval.push($(this).val());
            }
            else {
                //How to remove the Unchecked item from array..
            }
        });
    });

Thanks in advance.

Share Improve this question asked Jun 26, 2013 at 15:59 user937194user937194 3833 gold badges6 silver badges12 bronze badges 2
  • Take a look here: stackoverflow./questions/3596089/… – Irvin Dominin Commented Jun 26, 2013 at 16:01
  • 1 Why not generate the array whenever you need it? – Felix Kling Commented Jun 26, 2013 at 16:07
Add a ment  | 

1 Answer 1

Reset to default 7

If you have an array

var mylist = ['a','b','c','d'];

To remove the value 'b':

if ((index = mylist.indexOf('b')) !== -1)
    mylist.splice(index, 1);

Results in:

mylist == ['a','c','d'];

For your application:

 $(function () {
   var listval = [];
        $('input[name="checkedRecords"]').bind('click', function () {
            if ($(this).is(':checked')) {
                listval.push($(this).val());
            }
            else {
                if ((index = listval.indexOf($(this).val())) !== -1) {
                    listval.splice(index, 1);
                }
            }
        });
    });
发布评论

评论列表(0)

  1. 暂无评论