Как я могу создать массив из ввода и отправить его в URL, используя jQuery?

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? показать = 1: 2: 3

1: 2: 3 в качестве отмеченных полей

Теги:

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);
});

Демо: скрипка

Ещё вопросы

Сообщество Overcoder
Наверх
Меню