jquery 根据 option 的 text 定位选中 option
2024-10-09 04:46:06
$('#test option[text="b"]').attr("selected",true);
上面的方法在 jquery 低于 1.4.2 的版本(含)中有效,在更高版本中无效!
例如:
<select name="number" id="test">
<option value="">a</option>
<option value="">b</option>
</select>
jquery 版本
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
解决一:精确匹配,选择文本与所给字符串完全一样的option。
$('#test option').filter(function(){return $(this).text()=="b";}).attr("selected",true);
解决二:子串匹配,选择文本包含所给字符串的option。
$("#test option:contains('b')").attr('selected', true);
最新文章
- 总结-Hibernate
- linux命令-find {查找}
- XtraBackup原理解读
- Post model至Web Api创建或是保存数据
- C#制作验证码
- HDU 4782 Beautiful Soup --模拟
- 二级指针的作用及用途 .xml
- AVA取整以及四舍五入
- 检索表中所有列的名称、DB中的用户表
- JVM之---Java内存分配参数(第四篇)
- python【第三篇】函数
- 寻找失踪的整数数组(Find the missing integer)
- 【Sqlserver系列】初级思维导图
- 我从业11年来遇到的最奇葩的raid0+1数据恢复经历
- vue中computed、metfods、watch的区别
- 干货 | SSMS客户端连接京东云RDS SQL Server配置方法
- 子弹短信光鲜的背后:网易云信首席架构师分享亿级IM平台的技术实践
- Go For It ,一个灵活的待办事项列表程序
- angular2.0学习笔记7.echarts 地图(type:&#39;map&#39;)slice undifined 出错问题
- nginx mac 下启动 停止 重启,查看安装位置
热门文章
- Android:finish()与System.exit(0)之间的区别
- Scala的Higher-Kinded类型
- 记坑: ConfigurationProperties 和 RefreshScope
- webpack4.x开发环境配置
- C# 对 TCP 客户端的状态封装
- 基于tp3开发的cms-前台代码执行
- Spring Cloud 学习 之 Spring Cloud Eureka(架构)
- input在IOS中的聚焦问题
- [hdu5593 ZYB&#39;s Tree] 树上统计
- HDU 3874 Necklace 区间查询的离线操作