select 多选下拉取消选中的时候得到value和text

  • select 下拉框多选的时候,第一次选中一个option得到了相应的value和text,当我取消选中的这个option时有方法再次得到相应的value和text吗


慕的地6264312
浏览 1062回答 5
5回答

侃侃无极

下拉框加一个点击事件,每次点击都获取你打勾的value和text就可以了

UYOU

获得select框,完后往下找勾选的就可以了,写个循环,如果这个options有勾选的class,就把他的value和text拿出来放到数组里

30秒到达战场

说实话没太看懂LZ的意思,如果楼主是想选中完以后获取当前的value和text值,那么可以参照如下代码:<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <title>Title</title>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function change() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("change()事件触发了");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //获取当前的select对象&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let obj = document.querySelector(".sel");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index = obj.selectedIndex; // 选中索引&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var text = obj.options[index].text; // 选中文本&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var value = obj.options[index].value; // 选中值&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("value:"+value+",\ttext:"+text);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></head><body>&nbsp; &nbsp; <select class="sel" onchange="change() ">&nbsp; &nbsp; &nbsp; &nbsp; <option value="volvo">Volvo</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="saab">Saab</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="opel">Opel</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="audi">Audi</option>&nbsp; &nbsp; </select></body></html>

qq_笑_17

希望采纳,jquery取到select&nbsp;变化事件,保存上次的选中值,并且判断如果和上一次的值不一样就可以知道取消选中的值了<select&nbsp;id="myselect"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<option&nbsp;value="a">atext</option> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<option&nbsp;value="b">btext</option> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<option&nbsp;value="c">ctext</option> &nbsp;&nbsp;&nbsp;&nbsp;</select> <script> &nbsp;&nbsp;&nbsp;&nbsp;$(document).ready(function&nbsp;()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lastSelect_value&nbsp;=&nbsp;"";//上一次选中的value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lastSelect_text&nbsp;=&nbsp;"";//上一次选中的text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;thisSelect_value&nbsp;=&nbsp;"";//这次选中的value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;thisSelect_text&nbsp;=&nbsp;"";//这次选中的text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("#myselect").change(function&nbsp;()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thisSelect_value&nbsp;=&nbsp;$("#myselect").val(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thisSelect_text&nbsp;=&nbsp;$("#myselect").text(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(thisSelect_value&nbsp;!=&nbsp;lastSelect_value&nbsp;&&&nbsp;lastSelect_value&nbsp;!=&nbsp;"")&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//当这一次和上一次不同时,说明取消了某个选中 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("取消选中前的value是:"&nbsp;+&nbsp;lastSelect_value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("取消选中前的text是:"&nbsp;+&nbsp;lastSelect_text); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastSelect_value&nbsp;=&nbsp;thisSelect_value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastSelect_text&nbsp;=&nbsp;thisSelect_text; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;}) </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript