mercredi 6 mai 2015

JQuery, check box .val .change not working together

I'm making a website containing checkboxlist which gets its values from a database. (with Iterator)

If statement checks if Tayte_nimi equals "Juusto" or "Tomaattikastike". If true, then check box is checked. If not true then not..

In the script I want to print out the label (Tayte_nimi) when the check box is checked. Problem is that I'd like to have "Juusto" and "Tomaattikastike" already printed out. It works IF I change .change to .val but then when I check other check boxes, nothing happens. (Obvious)

Basically I want to have .change and .val to work. Putting them one after another, doesn't work either. Just adds empty lines and unchecking any of selected check boxes causes the whole list to disappear.

I'm a rookie, pls help me I cri evrtiem :'( Seriously I've spent like 8 hours trying to figure this out.. really frustrating

<div id="taytelista"></div>
        <%  if (tayte.getTayte_nimi().equals("Juusto") || tayte.getTayte_nimi().equals("Tomaattikastike")) {%>


            <li><input type="checkbox" checked name="Pizza_tayte" value=<%=tayte.getTayte_id()%> /> 
            <label for="ossm"><%=tayte.getTayte_nimi()%> </label></li>

        <%}else{%>

                 <li><input type="checkbox" name="Pizza_tayte" value=<%=tayte.getTayte_id()%> />
                 <label for="ossm"><%=tayte.getTayte_nimi()%> </label></li>
        <%}%>                

        <script src="http://ift.tt/rs8qB8"></script>
        <script>
        $('#taytelista input').change(function() {
              if (this.checked) {
                $li = $('<li></li>');
                $li.text(this.value);
                $('#results').append($li);
              }
              else {
                $('li:contains('+this.value+')', '#results').remove();
              }

        });


        </script>




Aucun commentaire:

Enregistrer un commentaire