mercredi 29 juillet 2015

Using checkboxes to exclude certain results using javascript

I have a list of things in an array the array consists of arrays of things, like lets say a recipe book or a dvd collection. so you would have an array containing your dvds which contain an array (array... title, length, genre...).

  1. I have a checkbox system that allows you to pick required fields and the javascript scans the checked checkboxes and if the value of the checkbox matches that of a dvd it will output it in the answer / submit.

  2. I need to however be able to select things i dont want and eliminate them from the output list. like choosing an actor you dont like, films with them in will not be outputted. ^ this is what I am struggling with. How do I combine 1. and 2. to run in one query.

I need the code to scan through the checkboxes checked and remove the results containing them from the list that will be outputted using the items they do want!

Yes, that is the best I can explain.

Any help or examples of code that will help would be greatly appreciated! Minds on the prize team! Thanks, J.

function myFunction() {
    $('#answer').toggle();
    $('#questions').toggle();

    var cb=$("input[type='checkbox']:checked");
    var foundCount=0;
    for(var i=0;i<dvds.length;i++){
        var found=false;
        var foundAct=0;
        var recp=dvds[i];

        for(var j=0;j<cb.length;j++){
            if( recp.actors.indexOf( cb[j].value ) > -1 ) {
                found=true;
                foundAct++;
            }
        }

        window.scrollTo(0, 0);
        recp.foundActCount=foundAct;

    }

    //http://ift.tt/19P8X1H
    dvds=dvds.sort( function(a,b){
        if( a.foundActCount>b.foundActCount ) {
            return -1;
        }
        if( a.foundActCount<b.foundActCount ) {
            return 1;
        }
        if( a.foundActCount==b.foundActCount ) {
            return 0;
        }
    } );

    for(var i=0;i<dvds.length;i++){
        var recp=dvds[i];

        if( recp.foundActCount > 0 ){
            foundCount++;
            $('#answer').append   ........

for(var k=0;k<recp.actors.length;k++){
                found=false;
                for(var j=0;j<cb.length;j++){
                    if( recp.actors[k] == cb[j].value ) {
                        found=true;
                    }
                }   

                if( found ){
                $('#'+recp.name.replace(//g,'_')).append(''+recp.actors[k]+' : &nbsp; <input class="ing roundedOne" type="checkbox" checked="checked"><br>');
                }else{     $('#'+recp.name.replace(//g,'_')).append(''+recp.actors[k]+' : &nbsp; <input class="ing roundedOne" type="checkbox"> <br>');
                }
            }
        }   
    }

    if( foundCount== 0 ){
        $('#noanswer').append   ......  .toggle();

Theres the code i have so far. now i need to remove the excluded checkboxes...




Aucun commentaire:

Enregistrer un commentaire