从多个选择中获取单击选项的索引或值

时间:2014-10-28 10:52:47

标签: javascript jquery html

我有多个选择,我想点击我选择的选项。

E.g。如果我点击第一个选项,则会显示第一个选项indexvalue

HTML代码

<select class="founder_designation" rel="Designation" placeholder="Designation" multiple="multiple" name="designation">
<option value="">-- Select Designation --</option>
<option value="chairman_managing_director">Chairman &amp; Managing Director</option>
<option selected="selected" value="director" >Director</option>
<option value="whole_time_director">Whole Time Director</option>
<option value="other">Other</option>
</select>

jQuery代码

$(".founder_designation").on("change",function(){
  alert($(this).val());   //It display all selected options with comma separated
})

JS Fiddle

3 个答案:

答案 0 :(得分:2)

这将解决问题,

$('.founder_designation').on('click','option',function(){
 alert($(this).val());
});

如果你想要所有的价值,那么试试这样的事情,

$('.founder_designation').on('click','option',function(){

    alert("clicked value:"+$(this).val());

    $a=$('.founder_designation option:selected');
    $str='';
    for($i=0;$i<$a.length;$i++){
        $str+=$('.founder_designation option:selected:eq('+$i+')').val()+",";
    }

    alert("all selected :"+$str);
});

检查演示 JS FIDDLE DEMO

答案 1 :(得分:1)

这应该可以解决问题:

$('.founder_designation option:selected').val();

如果你有多个名字为founder_designation的选项,你可以这样做:

$('.founder_designation option').click(function(e) {
   var value = $(this).val();
   alert(value);
});

答案 2 :(得分:1)

$(".founder_designation").on("change",function(e){
  alert(e.target.value);
  // OR
  alert($(e.target).val());
});