Domanda

I am able to get the value of checked checkbox by this query:

vals = $('input[name="checkedID"]:checked').map(function() {
    return this.value;
}).get().join(',');

But, how can I get the value of hidden checked checkbox? I've tried this but it doesn't work:

vals = $('input:hidden[name="checkedID"]:checked').map(function() {
    return this.value;
}).get().join(',');

Thanks!

È stato utile?

Soluzione 2

I found this answer on: http://datatables.net/forums/discussion/17382/form-checkbox-values-not-posting. I ended up using below script (with a few modification from the source):

<script type="text/javascript">
    $(document).ready( function () {
        var oTable = $('#idparent').dataTable();

        $('#form').submit(function () {
            $("input[name='checkedID[]:checked']").remove();
            $("input:checked", oTable.fnGetNodes()).each(function(){
                $('<input type="checkbox" id="checkedID" class="checkedID" name="checkedID[]" ' + 'value="' +
                  $(this).val() + '" type="hidden" checked="checked" />')
                    .css("display", "none")
                    .appendTo('#form');
            });
        });
    } );   
</script>

The value of checked checkbox need to re-append to the form on submit. Code above resulted in redundant value, so you need to remove the duplicate value after that.

Altri suggerimenti

It shouldn't matter if the checkbox is hidden or not.

$('input:hidden[name="checkedID"]:checked').val();

with:

<input type="checkbox" name="checkedID" value="Yes" style="display:none;" checked>

Returned what I expected. Here's a small fiddle of it: http://jsbin.com/kixujoci/2/

Keep in mind :hidden wouldn't work if the input isn't visible due to it being hidden by one of its parents.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top