I am trying to get option value on hover/mouseover event when option is hovered using chosen plugin....
Fiddle : Demo Fiddle
Here is js code...
$("#myselect").chosen();
$('#myselect').next('.chosen-container').on('mouseenter', 'li.active-result', function(e) {
alert($(this).text());
alert($(this).val()); // how to get option value...?
});
I am trying to get option value on hover/mouseover event when option is hovered using chosen plugin....
Fiddle : Demo Fiddle
Here is js code...
$("#myselect").chosen();
$('#myselect').next('.chosen-container').on('mouseenter', 'li.active-result', function(e) {
alert($(this).text());
alert($(this).val()); // how to get option value...?
});
Share
Improve this question
asked Jun 25, 2014 at 9:12
Developer DeskDeveloper Desk
2,3449 gold badges42 silver badges78 bronze badges
2
- you can't get the option value while its not yet selected. – Yaje Commented Jun 25, 2014 at 9:20
- ooh is it ? any other way to get option value? – Developer Desk Commented Jun 25, 2014 at 9:22
3 Answers
Reset to default 6USe delegate for that, because chosen plugin creates the .active-result
class elements dynamically.
$("#myselect").chosen();
$(document).on("hover",".active-result",function(){
alert($(this).text());
});
Fiddle
Edit
$(document).on("hover",".active-result",function(){
alert($("#myselect option").eq($(this).data("option-array-index")).val());
});
Updated fiddle
You need event delegation for binding the events to dynamically added DOM:
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.
$("body").on('mouseenter','li.active-result',function(){
alert($(this).data('option-array-index'));
});
Working Demo
As this plugin created new elements for options and to read option value you need find option matching text and read its value:
$('#myselect').next('.chosen-container').on('mouseenter', 'li.active-result', function(e) {
var currentText = $(this).text();
alert($(this).text());
alert($('#myselect option').filter(function () { return $(this).html() == currentText; }).val()); // how to get option value...?
});
Demo
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742289886a4415988.html
评论列表(0条)