使用jquery进行鼠标悬停时获取下拉选项的值

时间:2017-07-12 11:26:15

标签: jquery

我在html页面中有一个下拉列表,我需要使用j Query获取鼠标悬停项的值,请指教

我的下拉代码

<select name="select2" id="sub_groups" class="form-control input-md" onchange="getval(this);" >
                                                     <option data-group='SHOW' value="0" selected="selected">Select Turbine Type</option>
                                                      <option data-group='1' value="V1">V39/ V42/ V44/ V47</option>
                                                      <!--option data-group='1' value="V2">V42-600KW</option>
                                                      <option data-group='1' value="V3">V44-600KW</option>
                                                      <option data-group='1' value="V4">V47-660KW</option-->
                                                      <option data-group='1' value="V5">V52-850KW</option>
                                                      <option data-group='1' value="V6">V66-1.65MW/ 1.75MW</option>
                                                      <!--option data-group='1' value="V7">V66-1.75MW</option-->
                                                      <option data-group='1' value="V8">V80-V90 1.8-2.0MW, MK1-5</option>
                                                      <option data-group='1' value="V9">V80-V90 1.8-2.0MW, MK5.1,6-7</option>
                                                      <option data-group='1' value="V10">V80-V90 1.8-2.0MW, MK8</option>
                                                      <option data-group='1' value="V11">V82-1.65MW</option>
                                                      <option data-group='1' value="V12">V90-3MW</option>
                                                      <option data-group='1' value="V14">V100 1.8-2.0MW, MK7</option>
                                                      <option data-group='1' value="V15">V100 2.0MW, MK9</option>
                                                      <option data-group='1' value="V16">V100 2.0MW, MK10</option>
                                                      <option data-group='1' value="V18">V110-2.0MW, Mk10, 10B</option>
                                                      <option data-group='1' value="V17">V105-3.3/3.45MW, MK2C</option>   
                                                      <option data-group='1' value="V19">V112-3.0 MW, Mk0,0E,1A,1B</option>
                                                      <option data-group='1' value="V20">V112-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V21">V117-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V22">V126-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V23">NM52 / NM64(950KW, 1MW) </option>
                                                      <!--option data-group='1' value="V24">NM54-950KW</option>
                                                      <option data-group='1' value="V25">NM54-1MW</option-->

                                                    </select>  

和我的jquery

$("#sub_groups").hover(function() {
alert("hello");

});

当我鼠标悬停在任何下拉项目上时,我应该获取值 enter image description here

2 个答案:

答案 0 :(得分:2)

会是这样的:

&#13;
&#13;
$('#sub_groups').mousemove(function() {
  alert($(this).val());
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select2" id="sub_groups" class="form-control input-md" onchange="getval(this);">
                                                     <option data-group='SHOW' value="0" selected="selected">Select Turbine Type</option>
                                                      <option data-group='1' value="V1">V39/ V42/ V44/ V47</option>
                                                      <!--option data-group='1' value="V2">V42-600KW</option>
                                                      <option data-group='1' value="V3">V44-600KW</option>
                                                      <option data-group='1' value="V4">V47-660KW</option-->
                                                      <option data-group='1' value="V5">V52-850KW</option>
                                                      <option data-group='1' value="V6">V66-1.65MW/ 1.75MW</option>
                                                      <!--option data-group='1' value="V7">V66-1.75MW</option-->
                                                      <option data-group='1' value="V8">V80-V90 1.8-2.0MW, MK1-5</option>
                                                      <option data-group='1' value="V9">V80-V90 1.8-2.0MW, MK5.1,6-7</option>
                                                      <option data-group='1' value="V10">V80-V90 1.8-2.0MW, MK8</option>
                                                      <option data-group='1' value="V11">V82-1.65MW</option>
                                                      <option data-group='1' value="V12">V90-3MW</option>
                                                      <option data-group='1' value="V14">V100 1.8-2.0MW, MK7</option>
                                                      <option data-group='1' value="V15">V100 2.0MW, MK9</option>
                                                      <option data-group='1' value="V16">V100 2.0MW, MK10</option>
                                                      <option data-group='1' value="V18">V110-2.0MW, Mk10, 10B</option>
                                                      <option data-group='1' value="V17">V105-3.3/3.45MW, MK2C</option>   
                                                      <option data-group='1' value="V19">V112-3.0 MW, Mk0,0E,1A,1B</option>
                                                      <option data-group='1' value="V20">V112-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V21">V117-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V22">V126-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V23">NM52 / NM64(950KW, 1MW) </option>
                                                      <!--option data-group='1' value="V24">NM54-950KW</option>
                                                      <option data-group='1' value="V25">NM54-1MW</option-->

                                                    </select>
&#13;
&#13;
&#13;

$('#sub_groups').mousemove(function () {
        alert($(this).val());
 });

您使用内部的mouse move eventget the value

答案 1 :(得分:1)

我认为它是动态附加的元素。因此与on()

一起使用

&#13;
&#13;
$(document).on('mouseover','#sub_groups',function() {
  console.log($(this).val())

})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select2" id="sub_groups" class="form-control input-md" onchange="getval(this);">
                                                     <option data-group='SHOW' value="0" selected="selected">Select Turbine Type</option>
                                                      <option data-group='1' value="V1">V39/ V42/ V44/ V47</option>
                                                      <!--option data-group='1' value="V2">V42-600KW</option>
                                                      <option data-group='1' value="V3">V44-600KW</option>
                                                      <option data-group='1' value="V4">V47-660KW</option-->
                                                      <option data-group='1' value="V5">V52-850KW</option>
                                                      <option data-group='1' value="V6">V66-1.65MW/ 1.75MW</option>
                                                      <!--option data-group='1' value="V7">V66-1.75MW</option-->
                                                      <option data-group='1' value="V8">V80-V90 1.8-2.0MW, MK1-5</option>
                                                      <option data-group='1' value="V9">V80-V90 1.8-2.0MW, MK5.1,6-7</option>
                                                      <option data-group='1' value="V10">V80-V90 1.8-2.0MW, MK8</option>
                                                      <option data-group='1' value="V11">V82-1.65MW</option>
                                                      <option data-group='1' value="V12">V90-3MW</option>
                                                      <option data-group='1' value="V14">V100 1.8-2.0MW, MK7</option>
                                                      <option data-group='1' value="V15">V100 2.0MW, MK9</option>
                                                      <option data-group='1' value="V16">V100 2.0MW, MK10</option>
                                                      <option data-group='1' value="V18">V110-2.0MW, Mk10, 10B</option>
                                                      <option data-group='1' value="V17">V105-3.3/3.45MW, MK2C</option>   
                                                      <option data-group='1' value="V19">V112-3.0 MW, Mk0,0E,1A,1B</option>
                                                      <option data-group='1' value="V20">V112-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V21">V117-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V22">V126-3.3/3.45MW, MK2A-2C</option>
                                                      <option data-group='1' value="V23">NM52 / NM64(950KW, 1MW) </option>
                                                      <!--option data-group='1' value="V24">NM54-950KW</option>
                                                      <option data-group='1' value="V25">NM54-1MW</option-->

                                                    </select>
&#13;
&#13;
&#13;