vendredi 8 décembre 2017

Checked checkbox using array response in JavaScript

I have a form and it has 4 checkboxes. I am getting a response from the server and according to that response I need to checked that particular checkbox/s.

For example I have four checkboxes called 2100,2200,2300,2400

Then I am getting a array response from the server and array has 2200 and 2400. Then I need to checked both 2200 and 2400 checkboxes.

$.ajax({
    type: "POST",
    url: "send_db.php",
    data: {'send_array':array_send},
    success:  function(data){

    var jsonRes  = jQuery.parseJSON(data);
    var plant  = jsonRes.plant;
    var array_length = jsonRes.plant.length;

    for (var i = 0; i < array_length; i++) {

        document.getElementsByName("user_plant1").checked = true;

    }

    }
});

Here plant has the values.

These are my checkboxes...

<div class="form-group">
    <label class="checkbox-inline"><input type="checkbox" id="chk21" name="user_plant1" value="2100">2100</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk22" name="user_plant1" value="2200">2200</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk23" name="user_plant1" value="2300">2300</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk24" name="user_plant1" value="2400">2400</label>
</div>




Aucun commentaire:

Enregistrer un commentaire