mardi 17 janvier 2017

By clicking on box1 change value from box2

I need your help, I'm looking for a solution of: There is a form with several checkboxes, but only 4 of them are important. Here the html code from this 4:

<input name="opt[pool]" id="opt_pool" type="checkbox" class="searchval" value="1" onclick="submit_search()">Pool
<input name="opt[ppool]" id="opt_ppool" type="checkbox" class="searchval" value="1" onclick="submit_search()">PPool

<input name="opt[internet]" id="opt_internet" type="checkbox" class="searchval" value="1" onclick="submit_search()">Internet
<input name="opt[internetw]" id="opt_internetw" type="checkbox" class="searchval" value="1" onclick="submit_search()">InternetW

Here is the jQuery code:

function build_search(skip){
    var sparam = ''; 
    var fields = {};
    $('.searchval').each(function(index){          
        if($(this).attr('name')==skip) {}
        else if($(this).attr('type')=='checkbox'||$(this).attr('type')=='radio'){
            if($(this).prop("checked")){
                sparam+=$(this).attr('name')+"="+$(this).attr('value')+"&";
                fields[$(this).attr('name')] = $(this).attr('value');
            }
        }
        else if($(this).val()!=undefined&&$(this).val()!=''){
            sparam+=$(this).attr('name')+"="+$(this).val()+"&";
            fields[$(this).attr('name')] = $(this).val();
        }
    });
}

Now I would like to intervene - when clicked on pool, this checked with val=1 and ppool unchecked with val=0. And reverse when click on ppool htis checked with val=1 and pool unchecked with val=0. This works with my code:

var boxesP = $('#opt_pool,#opt_ppool').click(function(){
    boxesP.not(this).prop('checked', false);
    $("#opt_pool").val( $("#opt_pool")[0].checked ? "0" : "1" );
    $("#opt_ppool").val( $("#opt_ppool")[0].checked ? "0" : "1" );
});

But the problem is when i click to other checkboxes, the checked pool with val=1, remains checked but the val=0. Where is my mistake? Thanks.




Aucun commentaire:

Enregistrer un commentaire