用jquery为select取值和设值
来源:互联网 发布:奖状荣誉证书制作软件 编辑:程序博客网 时间:2024/05/10 01:15
<select id="aselect"> <option value="1">a</option> <option value="2">b</option> <option value="3">c</option> <option value="4">d</option></select>
select相关的操作还蛮麻烦的,在这里整理一下
获得当前选中
console.log($("#aselect").val());//当前的值console.log($("#aselect").find("option:selected").text());//当前text//当前的index(以下4个都可以)console.log($('#aselect').prop('selectedIndex'));console.log($('option:selected', '#aselect').index());console.log($("#aselect").find("option:selected").index());console.log($('#aselect option').index($('#aselect option:selected')));
设置当前选中
设置val为选中
$("#aselect").prop("value","2");$("#aselect").val("4");$("#aselect").get(0).value = 3;$("#aselect option[value='2']").attr("selected", true);
设置text为选中
网上看到这个方法但没成功,我猜是jquery的版本问题1
$("#aselect").find("option[text='b']").attr("selected",true);
没找到什么好方法,自己写了个
$("#aselect option").each(function(){if ($(this).text()=="b") { $(this).attr("selected",true); }})
设置index为选中
$("#aselect").get(0).selectedIndex=2;$("#aselect option").eq(2).attr("selected",true);
好多方法都是选择器的改变,掌握jquery选择器很重要
- 试验所用jquery版本:jquery-1.11.2.min.js ↩
0 0
- 用jquery为select取值和设值
- select取数据库值设为默认值
- jquery select 取值赋值
- jquery checkbox,select,select 取值
- 浅谈jquery关于select框的取值和赋值
- 浅谈jquery关于select框的取值和赋值
- 浅谈jquery关于select框的取值和赋值
- jquery关于select框的取值和赋值
- jquery关于select框的取值和赋值
- jquery中对于select框的取值和赋值
- jquery操作select(option)的取值,设置和选中
- 浅谈jquery关于select框的取值和赋值
- jquery radio取值,checkbox取值,select取值
- DataKeysName的设值和取值
- CKEDITOR取值和设值
- 利用Javascript取和设FCKeditor值
- 利用Javascript取和设FCKeditor值
- 利用Javascript取和设FCKeditor值
- [Android]实现带显示密码按钮的EditText(无内存泄露)
- leetcode:First Missing Positive (JAVA)
- 第二章Servlet入门
- 堆栈初体验1
- SQL server 实现有则更新,无则插入
- 用jquery为select取值和设值
- 事务隔离级别
- C++中cos,sin,asin,acos这些三角函数操作
- android高德地图自定义定位图标并具有类似陀螺仪旋转功能。
- python dataframe groupby
- 凌云志
- Android多线程合并——使用CountDownLatch干掉第三方SDK的回调函数
- 二维树状数组 POJ 1195 Mobile phones
- 【NOIP2016提高A组8.12】通讯