如何按值选择元素jQuery

时间:2016-08-14 12:18:17

标签: javascript jquery

我想更改我的复选框的属性,如果它的值等于另一个值,那么这是我的论文:

我使用symfony和AJAX所以我有两个twig,在第一个我有代码AJAX导致调用第二个: tousContacts.html.twig



{% extends '::basecontent.html.twig' %}

    {% block title %}
        {{ page_title }}
    {% endblock %}

{% block stylesheets %}
    <link href="{{ asset('assets/apps/css/todo-2.min.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('assets/global/plugins/select2/css/select2.min.css') }}" rel="stylesheet" type="text/css"/>
    <link href="{{ asset('assets/global/plugins/select2/css/select2-bootstrap.min.css') }}" rel="stylesheet" type="text/css"/>
{% endblock %}

{% block content %}

    <div class="portlet light bordered">
        <div class="portlet-title">
            <div class="caption">
                <i class="icon-user font-green"></i>
                <span class="caption-subject font-green sbold uppercase">{{ page_title }}</span>
            </div>
            <div class="actions">
                <div class="btn-group btn-group-devided">
                    {{ form_start(formliste, {'attr': {'id': 'form_env', 'class': 'form_remplirliste'}}) }}
                        <div class="input-group">
                            <div class="input-icon">
                                {{ form_row(formliste.libelle, {'attr': {'class': 'form-control'} }) }}
                            </div>
                            <div class="input-group-btn">
                                <button id="genpassword" class="btn btn-success" type="submit" id="form_save">
                                    <i class="fa fa-arrow-left fa-fw"/></i> Créer la liste
                                </button>
                            </div>
                            <div class="input-group-btn">
                                <a href="{{ path('liste_showEvListe', {'id': id }) }}" class="btn btn-outline green"><i class="fa fa-list"></i> Terminer</a>
                            </div>
                        </div>
                    {{ form_end(formliste) }}<br>

                    <input type="hidden" id="myHidden" value="0"/>

                    <div class="modal-b">
                        <img src="{{ asset('assets/global/img/loading-spinner-grey.gif') }}" alt="" class="loading">
                        <span> &nbsp;&nbsp;Chargement... </span>
                    </div>
                </div>
            </div>
        </div>
        {% form_theme form 'bootstrap_3_layout.html.twig' %}
        <div class="portlet-body form">
            <div class="todo-ui">
                <div class="todo-sidebar">
                    <div class="portlet light " style="margin-bottom: 0">
                        <div class="portlet-title">
                            <div class="caption" data-toggle="collapse" data-target=".todo-project-list-content">
                                <span class="caption-subject font-green-sharp bold uppercase">Recherche par </span>
                                <span class="caption-helper visible-sm-inline-block visible-xs-inline-block">click to view project list</span>
                            </div>
                        </div>
                        <div class="portlet-body todo-project-list-content" style="height: auto;">
                            <div class="todo-project-list">
                                <div class="form-group">
                                    {{ form_widget(form.search) }}
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="portlet light " style="margin-bottom: 0">
                        <div class="portlet-title">
                            <div class="caption" data-toggle="collapse" data-target=".todo-project-list-content2">
                                <span class="caption-subject font-green-sharp bold uppercase">Organismes </span>
                                <span class="caption-helper visible-sm-inline-block visible-xs-inline-block">click to view project list</span>
                            </div>
                            <div class="actions">
                                <div class="actions">
                                    <a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;">
                                        <i class="fa fa-plus"></i> </a>
                                </div>
                            </div>
                        </div>
                        <div class="portlet-body todo-project-list-content2" style="height: auto;">
                            <div class="todo-project-list">
                                <ul class="nav nav-stacked">
                                    {# <li class="no_item"><a href="javascript:;"> Aucun organisme spécifié </a></li> #}
                                    <li class="add_item">{{ form_widget(form.organismes) }}</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="portlet light " style="margin-bottom: 0">
                        <div class="portlet-title">
                            <div class="caption" data-toggle="collapse" data-target=".todo-project-list-content-tags3">
                                <span class="caption-subject font-red bold uppercase">Programmes </span>
                                <span class="caption-helper visible-sm-inline-block visible-xs-inline-block">click to view</span>
                            </div>
                            <div class="actions">
                                <div class="actions">
                                    <a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;">
                                        <i class="fa fa-plus"></i> </a>
                                </div>
                            </div>
                        </div>
                        <div class="portlet-body todo-project-list-content todo-project-list-content-tags3" style="height: auto;">
                            <div class="todo-project-list">
                                <ul class="nav nav-pills nav-stacked">
                                    {# <li class="no_item"><a href="javascript:;"> Aucun programme spécifié </a></li> #}
                                    <li class="add_item">{{ form_widget(form.programmes) }}</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="portlet light " style="margin-bottom: 0">
                        <div class="portlet-title">
                            <div class="caption" data-toggle="collapse" data-target=".todo-project-list-content-tags3">
                                <span class="caption-subject font-red bold uppercase"> Catégorie </span>
                                <span class="caption-helper visible-sm-inline-block visible-xs-inline-block">click to view</span>
                            </div>
                            <div class="actions">
                                <div class="actions">
                                    <a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;">
                                        <i class="fa fa-plus"></i> </a>
                                </div>
                            </div>
                        </div>
                        <div class="portlet-body todo-project-list-content todo-project-list-content-tags3" style="height: auto;">
                            <div class="todo-project-list">
                                <ul class="nav nav-pills nav-stacked">
                                    {# <li class="no_item"><a href="javascript:;"> Aucune catégorie spécifiée </a></li> #}
                                    <li class="add_item">{{ form_widget(form.categories) }}</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- END TODO SIDEBAR -->
                <!-- BEGIN TODO CONTENT -->
                <div class="todo-content">
                    <div class="portlet light ">
                        <div class="portlet-body">
                            <div class="row">
                                <div class="todo-tasklist contacts">
                                    <div class="col-md-12">
                                        <p class="todo-tasklist-date">Pas de résultats trouvés</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- END TODO CONTENT -->
            </div>
        </div>
    </div>

    <div class="modal fade" id="ajax" role="basic" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <img src="{{ asset('assets/global/img/loading-spinner-grey.gif') }}" alt="" class="loading"> <span> &nbsp;&nbsp;Chargement... </span>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javascripts %}
    <script src="{{ asset('assets/global/plugins/select2/js/select2.full.min.js') }}" type="text/javascript"></script>
    <script type="application/javascript">
        $(".modal-b").hide();
        // var data =new Array();
        var list = [];
        var listno = [];
        $(document).ready(function () {
            $('.menu-toggler.sidebar-toggler').trigger('click');

         
        });
        url = '{{ path('liste_contacts') }}';
        var query = {
            'search': '',
            'page': 1,
            'organismes': [],
            'programmes': [],
            'categories': []
        };
        updateListeContacts();
        function updateListeContacts() {
            var ws = url;
            ws += '?key_search=' + query['search'];
            ws += '&organismes_search=' + query['organismes'];
            ws += '&programmes_search=' + query['programmes'];
            ws += '&categories_search=' + query['categories'];
            ws += '&page=' + query['page'];
            $.get(ws, function (data) {
                $('.todo-tasklist>div').remove();
                $('.todo-tasklist').html(data);
                set_favaction_listner();
                set_paginator_listner();
                set_checkction_listner();
            });
        }
        $('li.add_item').hide();
        $('a.add_item').click(function (e) {
            e.preventDefault();
            var $this = $(this);
            if ($this.hasClass('add_item'))
                $(this).removeClass('add_item').addClass('close_item').find('i').removeClass('fa-plus').addClass('fa-close').parent().parent().parent().parent().parent().find('li.add_item').show();
            else if ($this.hasClass('close_item'))
                $(this).removeClass('close_item').addClass('add_item').find('i').removeClass('fa-close').addClass('fa-plus').parent().parent().parent().parent().parent().find('li.add_item').hide();
        });
        $("select[name='form[organismes]'],select[name='form[programmes]'],select[name='form[categories]']").prepend('<option value="0" selected disabled>choisir</option>');
        $("select[name='form[organismes]'],select[name='form[programmes]'],select[name='form[categories]']").select2({
            allowClear: true,
            placeholder: 'TEst',
            width: null
        }).on("change", function () {
            query['page'] = 1;
            var $this = $(this);
            var $container = $this.parent().parent();
            query[$this.attr('name').replace('form[', '').replace(']', '')].push($this.val());
            $container.find('li.no_item').hide();
            var $selectedItem = $this.find("option[value=" + $this.val() + "]");
            var $deleteIcon = $('<span class="badge badge-danger" data-id="' + $this.val() + '"><i class="fa fa-close"></i></span>');
            $deleteIcon.click(function () {
                removeSearch($(this));
            });
            $selectedItem.remove();
            $this.val(0);
            $this.trigger('change.select2');
            $container.parent().parent().parent().find('.close_item').trigger('click');
            var $li = $('<li />');
            var $a = $('<a href="javascript:;" />');
            var $span = $('<span class="badge badge-success"> 0 </span>');
            var $spanTxt = $('<span class="text">' + $selectedItem.text() + '</span>');
            $a.append($spanTxt);
            $a.prepend($deleteIcon);
            $a.prepend($span);
            $li.append($a);
            $container.append($li)
            updateListeContacts();
        });
        var typingTimer;                //timer identifier
        var doneTypingInterval = 500;  //time in ms, 5 second for example
        var $input = $("input[name='form[search]']");
        //on keyup, start the countdown
        $input.on('keyup', function () {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(doneTyping, doneTypingInterval);
        });
        //on keydown, clear the countdown
        $input.on('keydown', function () {
            clearTimeout(typingTimer);
        });
        //user is "finished typing," do something
        function doneTyping() {
            query['search'] = $input.val();
            query['page'] = 1;
            updateListeContacts();
        }
        function removeSearch($this) {
            $container = $this.parent().parent().parent();
            queryCible = query[$container.find('select').attr('name').replace('form[', '').replace(']', '')];
            query[$container.find('select').attr('name').replace('form[', '').replace(']', '')].splice(queryCible.indexOf($this.attr('data-id')), 1);
            //delete query[$container.find('select').attr('name').replace('form[','').replace(']','')][queryCible.indexOf($this.attr('data-id'))];
            $container.find('select').append('<option value="' + $this.attr('data-id') + '">' + $this.parent().find('.text').text() + '</option>');
            $this.parent().parent().remove();
            if ($container.find('li').length < 3) {
                $container.find('li.no_item').show();
            }
            updateListeContacts();
        }
        function set_paginator_listner() {
            $('.paginator button:not(.active)').click(function (ev) {
                ev.preventDefault();
                query['page'] = $(this).attr('data-page');
                updateListeContacts();
            });
        }
        function set_favaction_listner() {
            $('.set_fave').bind('click', function () {
                $this = $(this);
                $.get($(this).attr('data-url'), function (data) {
                    if (data.toString() == 'true') {
                        $this.parent().parent().find('.set_fave i').removeClass('fa-star-o').addClass('fa-star');
                    } else {
                        $this.parent().parent().find('.set_fave i').removeClass('fa-star').addClass('fa-star-o');
                    }
                });
            });
        }
        set_favaction_listner();
        function set_checkction_listner() {
            //list=$("input:checked").map(function () { return this.value; }).get();
                     
               list = $.merge($("input:checked").map(function () {
                return this.value;
            }).get(), list);
            list = unique(list);
            var all= $("input:checkbox").map(function () { return this.value; }).get();
            var i;
                for (i = 0; i < all.length; ++i) {
                 for (j = 0; j < list.length; ++j) {
                 if(all[i]==list[j]){
                  
                   var a = all[i];
                 console.log($('.checkbox[value = a]').val());
                 //$('.checkbox[value = a]').attr('checked','checked');
                 }
                }
                }
                   
            $('.checkbox').bind('change', function () {
                //var x = $("input:checkbox:checked").val();
                // $this = $(this);
                var x = $(this).val();
                if ($(this).is(':checked')) {
                    console.log(x);
                    list.push(x);
                    console.log(list);
                } else {
                    console.log(x);
                    list = jQuery.grep(list, function (value) {
                        return value != x;
                    });
                    console.log(list);
                }
            });
        }
        set_checkction_listner();
        function unique(list) {
            var result = [];
            $.each(list, function (i, e) {
                if ($.inArray(e, result) == -1) result.push(e);
            });
            return result;
        }
        $("#form_env").submit(function (ev) {
            ev.preventDefault();
            $(".modal-b").show();
            var x = $("#form_libelle").val();
            var id = {{id}};
            var hidden = $("#myHidden").val();
            $.ajax({
                type: 'post',
                url: '{{ path('enregistrer22_liste') }}',
                data: {
                    arrayy: list,
                    x: x,
                    ev: id,
                    hidden: hidden,
                },
                beforeSend: function () {
                },
                success: function (data) {
                    $(".modal-b").hide();
                    $("#myHidden").val(data['idliste']);
                    $("#form_save").attr('value', "modifier la liste");
                    //console.log(data['idliste']);
                }
            });
        });
    </script>
{% endblock %}
&#13;
&#13;
&#13;

第二根枝条只是一个复选框的联系人列表 分配给每个联系人

0 个答案:

没有答案