admin管理员组文章数量:1410681
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());
});
本文标签: javascriptjQuery select() method is not workingStack Overflow
版权声明:本文标题:javascript - jQuery select() method is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744804459a2626059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论