문제

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!

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top