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

javascript - How to GetChecked values and Remove unchecked in array - Stack Overflow

programmeradmin3浏览0评论

i use array for checkbox value, if i click on checkbox it gets values,and if i uncheck this checkbox it remove its value and not save in array,here is my Code..

function selectUser(sender) {
        if (sender.prop('checked'),true)
            assignedTo[assignedTo.length] = sender.id;   
        else {
            for (var i = 0; i < assignedTo.length; i++) {
                if (sender.id == assignedTo[i]) {
                    assignedTo.splice(i, 1);
                    break;
                }
            }
        }
    }

Now My issue is if checked it saves value and if i uncheck it did not remove value from array,any help

i use array for checkbox value, if i click on checkbox it gets values,and if i uncheck this checkbox it remove its value and not save in array,here is my Code..

function selectUser(sender) {
        if (sender.prop('checked'),true)
            assignedTo[assignedTo.length] = sender.id;   
        else {
            for (var i = 0; i < assignedTo.length; i++) {
                if (sender.id == assignedTo[i]) {
                    assignedTo.splice(i, 1);
                    break;
                }
            }
        }
    }

Now My issue is if checked it saves value and if i uncheck it did not remove value from array,any help

Share Improve this question edited Dec 3, 2014 at 6:06 shaN 82813 silver badges23 bronze badges asked Dec 3, 2014 at 5:44 Mohsin KhanMohsin Khan 1311 gold badge1 silver badge10 bronze badges 2
  • 1 Please provide the corresponding HTML or create a jsfiddle to demonstrate what's not working. – PeterKA Commented Dec 3, 2014 at 5:52
  • Why not just use .push() and .pop() to add and remove values from the array. – user3559349 Commented Dec 3, 2014 at 5:54
Add a ment  | 

2 Answers 2

Reset to default 6

The approach you've taken could be very tedious. You could use .map() you can re-evaluate the array every time a checkbox is checked or unchecked as follows:

$(':checkbox[name=test]').on('change', function() {
    var assignedTo = $(':checkbox[name=test]:checked').map(function() {
        return this.id;
    })
    .get();

    //Out for DEMO purposes only 
    $('pre.out').text( JSON.stringify( assignedTo ) );
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="test" value="1" id="c1"/> 1 <br/>
<input type="checkbox" name="test" value="2" id="c2"/> 2 <br/>
<input type="checkbox" name="test" value="3" id="c3"/> 3 <br/>
<input type="checkbox" name="test" value="4" id="c4"/> 4 <br/>
<input type="checkbox" name="test" value="5" id="c5"/> 5 <br/>
<input type="checkbox" name="test" value="6" id="c6"/> 6 <br/>
<input type="checkbox" name="test" value="7" id="c7"/> 7 <br/>

<!--output area for DEMO purposes only -->
<pre class="out"></pre>

why not clear the array and store it with new values

var values=[];

$(".cbx").click(function(){
    values=[];
    $(".cbx").each(function(){
        if($(this).is(":checked"))
            values.push($(this).val());
    });
    alert(values);
});

demo

or

var values=[];

$(".cbx").change(function(){
    if($(this).is(":checked"))
       values.push($(this).val());
    else{
       var x = values.indexOf($(this).val());
       values.splice(x,1);
    }
    alert(values);
});
发布评论

评论列表(0)

  1. 暂无评论