0

ページを並べ替えるフィルターに問題があります。

<?php
    $vendors = array(1 => 'vendor1', 2 => 'vendor2', 3 => 'vendor3');
    $vendorChecked = array(1,3);
?>

<html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>       
        <script type="text/javascript">
            $(document).ready(function(){

               $( "#vendorForm" ).submit(function( event ) {

                var url = 'index.php?show=';
                var vendors = $('#vendors').map(function(){return $(this).val();}).get();

                var redirect = url+encodeURIComponent(vendors);

                console.log(redirect);

                //window.location.href =  url+encodeURIComponent(vendors);
                });

            });
        </script>

    </head>
    <body>
            <div class="sub2">
                <form class="form-inline" id="vendorForm" method="post" style="margin-bottom: 0px; width: 80%">
                    <? foreach ($vendors as $k => $v): ?>
                    <label style="padding-right: 10px;" class="checkbox inline"><input <?=  in_array($k, $vendorChecked)?'checked':'' ?> type="checkbox" name="vendors[]" id="vendors" value="<?=$k?>"> <?=$v?></label>
                    <? endforeach; ?>
                    <button class="btn" type="submit"> Show</button>  
                </form>
            </div>
    </body>
</html>

フォームの送信後にリダイレクトしたいのですが、URLは次のようになります。

index.php?show=1:2:3

チェックされたフィールドとして 1:2:3

4

1 に答える 1

0

試す

$("#vendorForm").submit(function (event) {
    var url = 'index.php?show=';

    //You cannot not use id as ID must the unique, in thise case use the name attribute
    //Also use :checked to filter only checked checkboxes
    var vendors = $('input[name="vendors[]"]:checked').map(function () {
        return this.value;
    }).get();

    //use .join() to concatenate the values
    var redirect = url + encodeURIComponent(vendors.join(':'));

    console.log(redirect);

    //window.location.href =  url+encodeURIComponent(vendors);
});

デモ:フィドル

于 2013-11-11T10:05:26.700 に答える