/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

jQuery获取各种标签的文本和value值

* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com

 

正文


jQuery获取各种标签的文本和value值

1、select

<select id="test">
   <option value ="volvo">Volvo</option>
   <option value ="saab">Saab</option>
   <option value="opel">Opel</option>
   <option value="audi">Audi</option>
</select>
 
获取select 选中的 text :
     ("#test").find("option:selected").text();   获取select选中的 value:     ("#test").val();
 
获取select选中的索引:
     ("#test").get(0).selectedindex;   设置select: 设置select 选中的索引:     ("#test").get(0).selectedindex=index;//index为索引值
 
设置select 选中的value:
     ("#test").attr("value","normal“);     ("#test").val("normal");
     ("#test").get(0).value = value;   设置select option项:       ("#test").append("<option value='value'>text</option>");  //添加一项option
     ("#test").prepend("<option value='0'>请选择</option>"); //在前面插入一项option     ("#test option:last").remove(); //删除索引值最大的option
     ("#test option[index='0']").remove();//删除索引值为0的option     ("#test option[value='3']").remove(); //删除值为3的option
     ("#test option[text='4']").remove(); //删除text值为4的option     清空 select:       ("test").empty();

2、radio

<input type="radio" name="colors" id="red">红色<br>
<input type="radio" name="colors" id="blue">蓝色<br>
<input type="radio" name="colors" id="green">绿色
 
1.获取选中值,三种方法都可以:
 
(input:radio:checked).val()("input[type='radio']:checked").val();
 
("input[name=colors]:checked").val();2.Radio('input:radio:first').attr('checked', 'checked');
 
或者
 
(input:radio:first).attr(checked,true);attr("checked",checked)=attr("checked",true)=attr("checked",true)3.Radio('input:radio:last').attr('checked', 'checked');
 
或者
 
(input:radio:last).attr(checked,true);4.radio('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....
 
或者
 
(input:radio).slice(1,2).attr(checked,true);5.Radio("input:radio").eq(索引值).remove();索引值=0,1,2....
 
如删除第3个Radio:("input:radio").eq(2).remove();6.Radio('input:radio').each(function(index,domEle){
 
      //写入代码
 
});

3、span

span是块状元素,span不存在Value值,<span> 测试内容</span>
要是想取span的内容,$("span").text();

jquery给span赋值

span是最简单的容器,可以当作一个形式标签,其取值赋值方法有别于一般的页面元素。

//赋值

$("#spanid").html(value)

//取值

$("#spanid").text()

在js中

假设我有一个<span id="text"></span>

现在我要单击一个按钮后让span中显示“Hello,World!”。

<input  id="showBtn" type="button"  value="显示" onclick="click()"/>

<script type="text/javascript">

function click(){

document.getElementById("text").innerText="Hello,World!";

}

</script>

posted @   一品堂.技术学习笔记  阅读(1782)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示

目录导航