更改选择下拉菜单复选框

时间:2014-11-27 19:57:09

标签: javascript php html mysql laravel

我的脚本中有一个选择下拉列表,如果您点击一个entrie,您可以立即向网站添加一个过滤器,以按流派对电影进行排序。该剧本如下:

<select name="genres" class="form-control" data-bind="value: genre">
            <option value="">{{ trans('dash.genres') }}</option>

                @foreach ($options->getGenres() as $genre)
                    <option value="{{ strtolower($genre) }}">{{ $genre }}</option>
                @endforeach
        </select>

现在我想将选择下拉列表更改为复选框,我用fallowing

实现了这一点
<ul>
                    @foreach ($options->getGenres() as $genre)
                    <li><input type="checkbox"  name="genres"  data-bind="value: genre"  value="{{ strtolower($genre) }}"/>{{ $genre }}</li>
                    @endforeach
</ul>    

但如果我点击一个复选框没有任何反应,现在我想要它与单页过滤结果的关系。

选择下拉列表会立即在页面上添加带有js的过滤器,如何使用复选框执行此操作?任何想法如何改变或什么?

修改

这是单击选择

后流入的列表
<ul id="selected-genres" data-bind="foreach: params.genres" class="list-unstyled list-inline">
            <li data-bind="click: $root.removeGenre"><i class="fa fa-times"></i> <span     data-bind="text: app.utils.ucFirst($data)"></span></li>
</ul>

这是此功能的Js文件

(function($) {
'use strict'

app.viewModels.titles.index = {

    /**
     * Stores all the games in their original state.
     * 
     * @type Array
     */
    sourceItems: ko.observableArray(),

    /**
     * Latest genre user has selected.
     * 
     * @type string,
     */
    genre: ko.observableArray(),


    /**
     * Stores all parameters user is currently restricting titles query by.
     * 
     * @type {Object}
     */
    params: {

        /**
         * Currently selected sorting option.
         * 
         * @type ko.observable(String)
         */
        order: ko.observable('mc_num_of_votesDesc'),

        /**
         * Stores users query to filters games on.
         * 
         * @type ko.observable(String),
         */
        query: ko.observable().extend({ rateLimit: { method: "notifyWhenChangesStop", timeout: 400 } }),

        /**
         * Filter titles that were released after this date.
         * 
         * @type ko.observable(String),
         */
        after: ko.observable(),

        /**
         * Filter titles that were released before this date.
         * 
         * @type ko.observable(String),
         */
        before: ko.observable(),

        /**
         * Filter movies to only ones that specified actors appeared in.
         * 
         * @type string
         */
        cast: ko.observable(),

        /**
         * Filter titles by genres.
         * 
         * @type string,
         */
        genres: ko.observableArray(),

        type: ko.observable(),

        minRating: ko.observable(),

        maxRating: ko.observable(),
    },

    /**
     * Uri to hit for paginated results.
     * 
     * @type {String}
     */
    uri: 'titles',

    /**
     * Remove a genre from genres array.
     * 
     * @param  string genre
     * @return void
     */
    removeGenre: function(genre) {
        app.viewModels.titles.index.params.genres.remove(genre);
    },

    start: function(type) {
        var self   = this,
            genres = app.utils.getUrlParams('genre'),
            page   = window.location.hash,
            paginations = $('.index-pagination');

        self.params.type(type);

        //filter by genres if they're present in url params
        if (genres) {
            $.each(genres.replace(/ /g,'').split(','), function(i,v) {
                self.params.genres.push(v);
            });
        }

        //push a genre user selects to genres array
        self.genre.subscribe(function(value) {

            if (value) {
                self.params.genres.push(value);
            }

        });

        //set a page if we found any in url hash
        if (page) {
            app.paginator.currentPage(page.replace('#page-', ''));
        }

        //change current page on url hash change
        $(window).bind('hashchange', function(e, b, c) {
            var hash = location.hash.slice(1);

            if (hash.indexOf('page-') > -1) {
                app.paginator.currentPage(hash.replace('page-', ''));
            }       
        });

        app.paginator.start(app.viewModels.titles.index, '#content', 18).success(function(data) {
            paginations.pagination({
                items: app.paginator.totalItems(),
                itemsOnPage: app.paginator.perPage(),
                displayedPages: 8,
                selectOnClick: false,
            });
        });

        app.paginator.paginate.subscribe(function(promise) {

            promise.success(function(data) {
                paginations.pagination('updateItems', app.paginator.totalItems());
                paginations.pagination('updateItemsOnPage', app.paginator.perPage());
            });

        });

        //scroll top when botom pagination is used
        $('.bottom-pagination').on('click', function(e) {
            if ( ! $(e.target).parent().is('.active')) {
                var body = $('html, body');
                body.animate({scrollTop:0});
            }
        });
    },
};

})(jQuery);

它使用Knockout.js我不包括缩小的代码

感谢您帮助我

1 个答案:

答案 0 :(得分:2)

在您的JS文件中,请更改

$('select[name="genres"]').change(function() { ... });

为:

$('input[name="genres"]').change(function() { ... });

似乎复选框没有change侦听器。