views:

223

answers:

2

I have to populate a multi checkbox in a form from data taken from an XML file from a post function with jquery?

<input type="checkbox" name="mercato[]" id="mkt_0" value="A">A<input type="checkbox" name="mercato[]" id="mkt_1" value="B">B

Thanks in advance. ciao h

+2  A: 

Here's an example of getting an XML and parsing it via jQuery:

$.ajax({
        type: "POST",
        url: "some.xml",
        dataType: "xml",
        success: function(xml) {
            var node = $(xml).find('node');
            var attribute = $(xml).find('node').attr("attribute");
        //TODO: do something with data
    }
    });

You might also want to use $.each() for iterating through element collections.

Edit: and here's how to create some checkboxes assuming the returned XML looks like this:

<?xml version="1.0"?>
<RootElement>
    <CheckBox name="checkbox1">checked</CheckBox>
    <CheckBox name="checkbox1">checked</CheckBox>
    <CheckBox name="checkbox1"></CheckBox>
    <CheckBox name="checkbox1"></CheckBox>
    <CheckBox name="checkbox1">checked</CheckBox>
</RootElement>

The js would look like this:

$(xml).find('CheckBox').each(function(){
        var value = $(this).text(); // get the value
        var name = $(this).attr("name"); //get the name attribute
        $("#parent_div").append( //append to some parent container
                $("<input/>") // a new input element
                    .attr("type", "checkbox") //of type checkbox
                    .attr("name", name) // with given name
                    .attr("checked", value) // checked="checked" or checked=""
        )
    });
mcm69
I know parse XML to get data for input text radio and select but not for checkbox :-(
haltman
Thanks for answering mcm69 but I can't append data I already have a form I've just to set checks to the right checkbox and leave unchecked the otherthanks againciaoh.
haltman
A: 

Thaks your help I got solution!

That's my working script:

var forma=$("form");
                var elements=$("*", forma);
                $.each(elements,function(i){
                    var id = $(elements[i]).attr("id");
                    var tipo = $(elements[i]).attr("type");
                    var nome=$(elements[i]).attr("name");
                    var val=$(elements[i]).attr("value");
                    switch (tipo)
                    {
                        case "text" :
                            $(elements[i]).val($(id, xml).text());
                        break;
                        case "radio":
                            $("input:radio[name='"+nome+"'][value='"+ $(nome, xml).text() +"']").attr('checked', true);
                        break;
                        case "checkbox":
                            $(xml).find(nome).each(function(){ 
                            var value =$(this).text();
                            if (val==value)
                            {
                                    $("#"+id).attr("checked", value);                                   }
                            }); 

                        break;
                        case "select-one" :
                            $(elements[i]).append("<option selected>"+$(nome ,xml).text()+"</option>");
                        break;
                    }

                    $("#scheda_ris").append(id+" "+tipo+" "+nome+" "+val+"<br>"); //that's just for debug
                });

If you have suggestions to make it more elegant I'll be really happy Thanks again

ciao h.

haltman