简体   繁体   English

我需要在选项标签中切换选择元素

[英]I need to toggle a select element in option tag

Am looking to toggle a select element. 我正在寻找切换选择元素。 I have a Select tag as follows :- 我有一个Select标签,如下所示:-

 <select id="orgAssign" multiple="true">
   <option selected="" value="10047">test1</option>
   <option selected="" value="10046">test2</option>
   <option value="10033">test3</option>
   <option value="10032">test4</option>
</select>

As seen from the html, the selected options have values, 10047 & 10046. This is native select from the browser. 从html可以看到,所选选项的值分别为10047和10046。这是浏览器的本机选择。 I want to toggle the options. 我想切换选项。 That is When the user clicks on them the first time, they are highlighted(selected) & when the user clicks on the same element the second time the element is unhighlighted(deselected). 也就是说,当用户第一次单击它们时,它们将突出显示(选中),而当用户第二次单击同一元素时,该元素将不被突出显示(取消选中)。

The jquery am trying to achieve this is as follows:- jQuery正在尝试实现以下目标:

highlightOrg: function(event){

    var $el = $(event['target']);

    $el.toggle().css({'background-color':'#dadfff'});

}

The element is highlighted(selected), but when the user clicks on it the second time, it is not unhighlighted(deselected). 该元素被突出显示(选中),但是当用户第二次单击该元素时,该元素并不是未突出显示(取消选中)。 Any help is appreciated. 任何帮助表示赞赏。

Instead of trying to overload a select to do something counter-intuitive to the end user, why not make your own input element? 与其尝试使选择超载以使最终用户做违反直觉的事情,不如自己创建输入元素?

You can simply use styled div elements to present the data to the user, and a little JavaScript/jQuery to handle it: 您可以简单地使用样式化的div元素将数据呈现给用户,并使用一些JavaScript / jQuery来处理它:

<div id="orgAssign" class="selectGroup">
   <div class="selectItem selected" data-value="10047">Test 1</div>
   <div class="selectItem selected" data-value="10046">Test 2</div>
   <div class="selectItem" data-value="10033">Test 3</div>
   <div class="selectItem" data-value="10032">Test 4</div>
</div>

jQuery to toggle elements: jQuery切换元素:

$('.selectItem').on('click', function() {
    $(this).toggleClass('selected');
    getValues();
});

jQuery to get values: jQuery获取值:

function getValues() {
    var values = [];
    $('#orgAssign .selected').each( function() {
           values.push($(this).data('value'));
    });

    return values;
}

JSFiddle Demo JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果选择了 select 标记中的选项,我如何创建元素 - How do I create an element if the option in select tag is selected <select>元素并观察(&#39;单击&#39;)选项以切换 option.selected - <select> element and observe('click') on options to toggle option.selected 如何将 select 标签中的选项传递给另一个 select 标签? - How can I pass an option in a select tag to another select tag? 我需要使用Twitter Bootstrap图标在数据表中使用切换选项 - I Need to Use toggle option in datatable using Twitter Bootstrap icons 我需要 select 一个 ul 元素 - I need to select a ul element 需要将选择标签选项值传递给ajax函数 - need to pass select tag option value to ajax function 选择标记元素的HTML不能反映它的选择选项 - Html of select tag element doesn't reflect it's selected option 从 select 元素获取超过 option 标签的值 - Getting more than the value of option tag from select element 如何为 select 标签内的选项元素添加鼠标悬停事件? - How to add mouseover event for an option element inside a select tag? 使用Dalekjs测试工具,当Option标记中没有“值”属性时,如何在Dropdown(选择元素)中选择Option? - Using Dalekjs test tool, how to select Option in Dropdown(Select element) when there is no “value” attribute in Option tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM