宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

js根据name获取所有的值

2022年3月8日 1072点热度 0人点赞 0条评论

通过name获取相同name的值:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>

<body>
  <form>
    <input name="a" value="121" /> <input name="a" value="234" /> <input name="a" value="345" /> <input name="a" value="456" /> <button>dddd</button>
  </form>
</body>
</html>

方法有两种:

第一种:是通过document对象进行操作

function te() {
    var aa = document.getElementsByName("a");
    for (var i = 0; i < aa.length; i++) {
        alert(aa[i].value);
    }
}

第二种:是通过jquery进行操作

function te() {
    $("[name=a]").each(function () {
        alert($(this).val());
    });
}

原文链接:https://blog.csdn.net/TOP__ONE/article/details/64439806

标签: JavaScript
最后更新:2022年3月9日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2