mercredi 27 avril 2016

Check if checkbox is checked and first in his group (index)

Two days past on this problem and no solution found.

Here my code

    <ul id="main">
        <li><input type="checkbox" name="item1 main" id="item1" value="Allergy" /><label for="item1">Allergy</label></li>

        <li>
            <ul class="sub">
                <li><input type="checkbox" name="item1-1" id="item1-1" value="Allergic rhinitis" /><label for="item1-1">Allergic rhinitis</span><br/></li>
                <li><input type="checkbox" name="item1-2" id="item1-2" value="Asthma" /><label for="item1-2">Asthma</span><br/></li>
                <li><input type="checkbox" name="item1-3" id="item1-3" value="Atopic dermatitis" /><label for="item1-3">Atopic dermatitis</span><br/></li>
                <li><input type="checkbox" name="item1-4" id="item1-4" value="Food allergy" /><label for="item1-4">Food allergy</span><br/></li>
            </ul>
        </li>


        <li><input type="checkbox" name="item2 main" id="item2" value="Anesthesiology" /><label for="item2">Anesogy</label>

            <li>
                <ul class="sub">
                    <li><input type="checkbox" name="item2-1" id="item2-1" value="Coagulation and transfusion" /><label for="item2-1">Coagulation and transfusion</span><br/></li>
                    <li><input type="checkbox" name="item2-2" id="item2-2" value="Pain" /><label for="item2-2">Pain</span><br/></li>
                    <li><input type="checkbox" name="item2-3" id="item2-3" value="Sepsis" /><label for="item2-3">Sepsis</span><br/></li>
                    <li><input type="checkbox" name="item2-4" id="item2-4" value="Sleep" /><label for="item2-4">Sleep</span><br/></li>
                </ul>
            </li>   
        </li>
        <li>
            <input type="checkbox" name="item3 main" id="item3" value="Cardiology" /><label for="item3">Cardiology</label>

            <li>    
                <ul class="sub">
                    <li><input type="checkbox" name="item3-1" id="item3-1" value="Acute coronary syndrome" /><label for="item3-1">Acute coronary syndrome</span><br/></li>
                    <li><input type="checkbox" name="item3-2" id="item3-2" value="Arrhythmia" /><label for="item3-2">Arrhythmia</span><br/></li>
                    <li><input type="checkbox" name="item3-3" id="item3-3" value="Coronary artery disease" /><label for="item3-3">Coronary artery disease</span><br/></li>
                    <li><input type="checkbox" name="item3-4" id="item3-4" value="Dyslipidemia" /><label for="item3-4">Dyslipidemia</span><br/></li>
                    <li><input type="checkbox" name="item3-5" id="item3-5" value="Heart failure" /><label for="item3-5">Heart failure</span><br/></li>
                    <li><input type="checkbox" name="item3-6" id="item3-6" value="Hypertension" /><label for="item3-6">Hypertension</span><br/></li>
                    <li><input type="checkbox" name="item3-7" id="item3-7" value="Valve disease" /><label for="item3-7">Valve disease</span><br/></li>
                </ul>
            </li>   
        </li>

    </ul>

I get the checked checkboxes via two serializeArray() functions in order to obtain a list of main items and a list of sub items. I would like to achieve to target the first checkbox which is checked in each sublist, example if i choose Asthma, i would get this. Problem is i make this in a "each" function

$.each(postData, function (i, field) {
    //MAIN ITEMS
    if (field.name.indexOf("main") != -1) {
        field.value = "\n" + field.value;
        field.value += "&#09;"
    }
    // SUB ITEMS
    else {
        if ($this.is($this.parent().parent().find("li > input[type=checkbox]:checked:first-child"))) {
            alert("FIRST-child : " + $this.val())
        };

        $this.parent().parent().find("li > input[type=checkbox]:checked").index($this);

        if (index == 1) {
            field.value = "000" + field.value;
        } else {
            field.value = "001-\n&#09;" + field.value;
        };
    };

    $("#resul").append(field.value);
});

I tried two different ways here to get the result I want but that always refer to the last checkbox cliked, impossible to target the first. Hope to be clear, excuse my english !

Thank for any help provided.




Aucun commentaire:

Enregistrer un commentaire