I need to get data for all selected options in select tag and for that I used onclick() function which gives the data of only clicked options. But if user selects all options with CTRL*A then no data will be received. I have tried to use select() which is not working in this case.
//jQuery onclick()
$('select[name=sensors]').on('click', function(){
$('#demo').text($('select[name=sensors]').val());
});
//jQuery select()
$('select[name=sensors]').select(function(){
$('#demo2').text($('select[name=sensors]').val());
});
<script src=".1.1/jquery.min.js"></script>
<select type='list' name='sensors' multiple>
<option value= "e11">e11</option>
<option value= "e12">e12</option>
<option value= "e13">e13</option>
<option value= "e14">e14</option>
</select>
<!--jQuery onclick()-->
<div id="demo"></div>
<!--jQuery select()-->
<div id="demo2"></div>
I need to get data for all selected options in select tag and for that I used onclick() function which gives the data of only clicked options. But if user selects all options with CTRL*A then no data will be received. I have tried to use select() which is not working in this case.
//jQuery onclick()
$('select[name=sensors]').on('click', function(){
$('#demo').text($('select[name=sensors]').val());
});
//jQuery select()
$('select[name=sensors]').select(function(){
$('#demo2').text($('select[name=sensors]').val());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select type='list' name='sensors' multiple>
<option value= "e11">e11</option>
<option value= "e12">e12</option>
<option value= "e13">e13</option>
<option value= "e14">e14</option>
</select>
<!--jQuery onclick()-->
<div id="demo"></div>
<!--jQuery select()-->
<div id="demo2"></div>
Share
Improve this question
asked Jul 22, 2015 at 13:16
RegarBoyRegarBoy
3,5211 gold badge25 silver badges47 bronze badges
4
-
1
The
select
event is triggered when the user selects (highlights) text in applicable control elements, not when an option is selected. – George Commented Jul 22, 2015 at 13:19 -
1
select
scope is limited toinput
&text-area
only..It wont work on div – Jayababu Commented Jul 22, 2015 at 13:21 - Jayababu api.jquery./select Here it is used on div – RegarBoy Commented Jul 22, 2015 at 13:23
-
It is unfortunate that they used
select
for inputs other than aselect
... Which just leads to confusion :) – iCollect.it Ltd Commented Jul 22, 2015 at 13:23
2 Answers
Reset to default 5Don't bind on click
, but on change
. This way even the changes ing from other kinds of interaction will be taken into account:
$('select[name=sensors]').on('change', function(){
$('#demo').text($('select[name=sensors]').val());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select type='list' name='sensors' multiple>
<option value= "e11">e11</option>
<option value= "e12">e12</option>
<option value= "e13">e13</option>
<option value= "e14">e14</option>
</select>
<div id="demo"></div>
As for your experimentation with select
, here's what the documentation says:
The select event is sent to an element when the user makes a text selection inside it. This event is limited to fields and boxes.
It's simply not relevant here, as the user isn't selecting text but options.
Do like this
$('select[name=sensors]').change(function(){
$('#demo2').text($(this).val());
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744804460a4594688.html
评论列表(0条)