本文主要介绍了jQuery实现select下拉框获取当前选中文本、值、索引以及添加/删除Select的Option项的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧,希望能帮助到大家。
话不多说,请看代码:
//直接保存后缀.htnl用谷歌浏览器打开,亲测有效 <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> $(function(){ //为Select添加事件,当选择其中一项时触发 $("select:eq(0)").change(function(){ //code }); //获取Select选中的Text:结果是由所有匹配元素包含的文本内容组合起来的文本 var checkText = $("select:eq(0) :selected").text();//建议用这个简单 = $("select:eq(0) option:selected").tetx(); = $("#One").find(":selected").text(); = $("#One").find("option:selected").text(); //如果多选,将返回一个数组,其包含所选的值。 var checkValue = $("#select_id").val(); //获取Select选中匹配元素的当前值,即[即使多选也只]取得第一个匹配元素的val内容 var checkValue = $("select:eq(0) :selected").val();//=========强烈建议用这个,以防多选 //获取Select选中的索引值 var checkIndex = $("#select_id ").get(0).selectedIndex; //获取Select最大的索引值 var maxIndex = $("#select_id :last").prop("index"); //建议用这个 = $("#select_id option:last").prop("index"); = $("select:eq(0)").find(":last").prop("index") = $("select:eq(0)").find("option:last").prop("index") //========================================================================================= //jQuery设置Select选择的 Text和Value: // 设置Select的Value值为4的项选中 $("#select_id ").val(4); //用这个 $("#select_id [value='4']").prop("selected", true); $("#select_id option[value='4']").prop("selected", true); //设置select中的第一个option被选中 $("select :first").prop("selected", true);//这个 $("select :first").prop("selected", 'selected'); $("select option:first").prop("selected", "true"); $("select option:first").prop("selected", "selected"); //============================================================================================ //jQuery添加/删除Select的Option项 $("#select_id").append("<option value='Value'>Text</option>"); //为Select末尾追加一个Option(下拉项) $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select首部插入一个Option(第一个位置) $("#select_id :last").remove(); //删除Select中索引值最大Option(最后一个) $("#select_id :fist").remove(); //删除Select中索引值最小为0Option(第一个) $("#select_id [value='3']").remove(); //删除Select中Value='3'的Option }); </script> </head> <table> <tr> <td> <!--multiple设定下拉框可以多选,size设定下拉框不呈现下拉方式,--> <select size="12" id="One" multiple="multiple"> <option value='1'>苹果</option> <option value="2">香蕉</option> <option value="3">草莓</option> <option value="4">橘子</option> </select> </td> <td> <input type="button" value=">>>"><br> <input type="button" value=" > "><br> <input type="button" value=" < "><br> <input type="button" value="<<<"><br> </td> <td> <select size="12" id="two" multiple="multiple"> <option value="5">葡萄</option> </select> </td> <td> <input type="button" value=" up "><br><br> <input type="button" value="down"><br> </td> </tr> </table>
相关推荐:
html中华支持分类Select下拉框的Optgroup标签
以上是jQuery实现select下拉框获取当前选中的文本的详细内容。更多信息请关注PHP中文网其他相关文章!

javaandjavascriptaredistinctlanguages:javaisusedforenterpriseandmobileapps,while javascriptifforInteractiveWebpages.1)JavaisComcompoppored,statieldinglationallyTypted,statilly tater astrunsonjvm.2)

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用