jeudi 22 octobre 2015

Deleting value in hiddenfield when checkbox is unchecked jquery

I am pretty new with jquery and javascript. But i have made this function. When a checkbox is checked, it's value get set in a hiddenfield. Here is the function.

<script>
    $('#checkDk').on('change', function () {
        $('#MainContent_hiddenTargetDk').val($(this).val());
        console.log($("#MainContent_hiddenTargetDk").val());
    });
</script>

I dont know if this is the best way of doing it, but it works. The problem is when the checkbox is unchecked the value does not get deleted from the hiddenfield. I have been searching a bit around and found this.

var value = this.checked ? this.value : "";

Can you somehow use this to check if it is checked or not? And if yes how do i incorporate it in my function?

Here is the checkbox html.

<input id="checkDk" type="checkbox" value="208" onselect="getvalue()" autocomplete="off">




Aucun commentaire:

Enregistrer un commentaire