在选项框中选择活动项目

时间:2016-05-19 11:47:55

标签: html select twig option

我有一个"选择"在我的网站的移动版本中。 这是代码:

<select class="form-control" id="selectGift">
    {% for category in rewards_categories %}
            <option id="{{category.tag}}" class=" BarCategory Bar{{category.tag}}" value="/xxxx/{{ category.id }}/{{ category.categoryName}}">
                {{category.categoryName}}
            </option>
        {% endfor %}
</select>

你怎么能看到我在我的模板中使用Twig扩展:) 问题是,当我更改选项重定向工作但不会更改活动列表项时,始终是第一个..

这是我的JS代码:

$('#selectGift').bind('change', function () {
var url = $(this).val();
window.location = url;
$(this).find('option:selected').addClass('active'); });

我做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

您需要使用:selected代替:active

var id = $(this).find('option:selected').attr('id');

答案 1 :(得分:0)

我删除了twig如果cicle,现在html是:

<select class="form-control" id="selectGift">
    {% for category in rewards_categories %}
            <option id="{{category.tag}}" class=" BarCategory Bar{{category.tag}}" value="/xxxx/{{ category.id }}/{{ category.categoryName | lower }}">
                {{category.categoryName}}
            </option>
        {% endfor %}
</select>

如何在选择不同选项并将其设置为活动时更改页面?