flexicon
flexicon

Reputation: 102

jquery checked checkbox for multiple load()

I need to load external file with checkbox

HTML Code

<div id="checkboxes">
            <form class="form_categoria">
                <input name="primo" type="checkbox" value="1">primo<br>
                <input name="secondo" type="checkbox" value="2">secondo<br>
                <input name="terzo" type="checkbox" value="3">terzo     
            </form>
        </div>
<form class="elenco">
        N: <div id="risultato_lista"></div>
    </form>

jQuery Code

$(document).ready(function(){
        $('input[type="checkbox"]').change(function() {
            //var checkbox_value = "";
            $('input[type=checkbox]').each(function () {
            var ischecked = $(this).is(":checked");
                if (ischecked`enter code here`) {
                    checkbox_value = $(this).val();
                    $('#risultato_lista').load(checkbox_value + '.php');
                }
            });
        //alert(checkbox_value);
        });
    });

But the code load only the last checked checkbox. I need to load all external .php files selected by the checkboxes. How to solve it?

Upvotes: 0

Views: 627

Answers (1)

Bhavin Solanki
Bhavin Solanki

Reputation: 4818

This is overwrite your div content every time.

 $('#risultato_lista').load(checkbox_value + '.php');

So you need to store load content in some variable and then append to $('#risultato_lista')

Instead of load i am here using $.get

$(document).ready(function(){
    $('input[type="checkbox"]').change(function() {
        $('#risultato_lista').html('');
        //var checkbox_value = "";
        $('input[type=checkbox]').each(function () {
        var ischecked = $(this).is(":checked");
            if (ischecked`enter code here`) {
                checkbox_value = $(this).val();
                $.get(checkbox_value + '.php', function(response) {
                    //var logfile = response;
                    $('#risultato_lista').append(response);
                });

            }
        });
    //alert(checkbox_value);
    });
});

Upvotes: 1

Related Questions