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