JQuery获取和设置select下拉框的值
2024-08-25 04:41:31
获取Select :
获取select 选中的 text :
$("#sid").find("option:selected").text(); 获取select选中的 value:
$("#sid").val(); 获取select选中的索引:
$("#sid").get(0).selectedIndex; 设置select:
设置select 选中的索引:
$("#sid").get(0).selectedIndex=index;//index为索引值 设置select 选中的value:
$("#sid").attr("value","Normal“);
$("#sid").val("Normal");
$("#sid").get(0).value = value; 设置select 选中的text:
var count=$("#sidoption").length;
for(var i=0;i<count;i++)
{ if($("#sid").get(0).options[i].text == text)
{
$("#sid").get(0).options[i].selected = true; break;
}
} $("#select_id option[text='jQuery']").attr("selected", true); 设置select option项: $("#select_id").append("<option value='Value'>Text</option>"); //添加一项option
$("#select_id").prepend("<option value='0'>请选择</option>"); //在前面插入一项option
$("#select_id option:last").remove(); //删除索引值最大的Option
$("#select_id option[index='0']").remove();//删除索引值为0的Option
$("#select_id option[value='3']").remove(); //删除值为3的Option
$("#select_id option[text='4']").remove(); //删除TEXT值为4的Option 清空 Select :
$("#sid").empty(); 获取radio的值:
<script language="javascript">
$(function() {
$("#Button1").click(function() {
var r = $("input:checked").attr("id");
alert(r); r = $(":radio:checked").attr("id");
alert(r); r = $(":radio:checked").eq(0).attr("id");
alert(r); r = $("#div1").children(":radio:checked").attr("id");
alert(r); r = $("#div1").children(":radio:checked").eq(0).attr("id");
alert(r); r = $("#div1").find(":radio:checked").eq(0).attr("id");
alert(r); $("#div1").find(":radio").each(function() {
alert($(this).attr("checked"));
if ($(this).attr("checked")) {
r = $(this).attr("id");
alert(r);
}
});
});
});
</script> <div id='div1'>
<input id="Radio1" name='rad' type="radio" />1<br/>
<input id="Radio2" name='rad' type="radio" />2
</div>
<input id="Button1" type="button" value="button" />
最新文章
- 关于localstorage的几个测试
- Oauth 2.0第三方账号登录原理图
- iOS-Gdata XML解析配置和简单使用
- 队列 - 从零开始实现by C++
- kafka中server.properties配置文件参数说明
- schedule和scheduleUpdate
- jQuery-ui treegird 使用
- 【6】了解Bootstrap栅格系统基础案例(1)
- Hot-Bar 軟板設計注意事項
- error C2018: unknown character &#39;0xa1&#39;
- [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
- 【BZOJ1834】 网络扩容
- 剑指Offer——搜狐畅游笔试题+知识点总结
- Salesforce的数据权限机制
- kernel中文件的读写操作可以使用vfs_read()和vfs_write
- hadoop hive install (5)
- Mongo命令批量更新某一数组字段的顺序
- linux centos 7 下安装ElasticSearch5.4
- WC2018伪题解
- web常见问题排查