1. 首页
  2. 技术知识

【WordPress相关】使用 jQuery 对下拉菜单 SELCET 进行增加、删除和修改的操作

作品分类:Wordpress相关  下拉  删除  获取  下拉  菜单  删除  增加  修改

使用 jQuery 对下拉菜单 SELCET 进行增加、删除和修改的操作,

jQuery 获取下拉菜单 SELECT 选择的 Text 和 Value:

//获取Select选择的Text
var checkText=jQuery("#select_id").find("option:selected").text();   

//获取Select选择的option Value
var checkValue=jQuery("#select_id").val();

//获取Select选择的索引值
var checkIndex=jQuery("#select_id ").get(0).selectedIndex;

//获取Select最大的索引值
var maxIndex=jQuery("#select_id option:last").attr("index");

jQuery 添加或者删除 下拉菜单 Select 的 Option 项:

<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

//为Select追加一个Option(下拉项)
jQuery("#select_id").append("<option value='Value'>Text</option>");

//为Select插入一个Option(第一个位置)
jQuery("#select_id").prepend("<option value='0'>请选择</option>");

//删除Select中索引值最大Option(最后一个)
jQuery("#select_id option:last").remove();

//删除Select中索引值为0的Option(第一个)
jQuery("#select_id option[index='0']").remove();  

//删除Select中Value='3'的Option
jQuery("#select_id option[value='3']").remove();

//删除Select中Text='4'的Option
jQuery("#select_id option[text='4']").remove();

清空下拉菜单的内容:

jQuery("#select_id").empty();

本站推荐使用的主机:,国外主机建议使用

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/32300.html

联系我们