本文翻译自:How can I select an element by name with jQuery?

Have a table column I'm trying to expand and hide: 有一个表格列我正在尝试展开和隐藏:

jQuery seems to hide the td elements when I select it by class but not by element's name . 当我按类而不是按元素名称选择时,jQuery似乎隐藏了td元素。

For example, why does: 例如,为什么:

$(".bold").hide(); // selecting by class works
$("tcol1").hide(); // select by element name does not work

Note the HTML below, the second column has the same name for all rows. 请注意下面的HTML,第二列对所有行都具有相同的名称。 How could I create this collection using the name attribute? 如何使用name属性创建此集合?

<tr>    <td>data1</td><td name="tcol1" class="bold"> data2</td>
</tr>
<tr>    <td>data1</td><td name="tcol1" class="bold"> data2</td>
</tr>
<tr>    <td>data1</td><td name="tcol1" class="bold"> data2</td>
</tr>

#1楼

参考:https://stackoom.com/question/4e2O/如何使用jQuery按名称选择元素


#2楼

你可以使用它的ID属性在JQuery中获取元素,如下所示:

$("#tcol1").hide();

#3楼

You can use the attribute selector: 您可以使用属性选择器:

$('td[name=tcol1]') // matches exactly 'tcol1'$('td[name^=tcol]') // matches those that begin with 'tcol'$('td[name$=tcol]') // matches those that end with 'tcol'$('td[name*=tcol]') // matches those that contain 'tcol'

#4楼

You could get the array of elements by name the old fashioned way and pass that array to jQuery. 你可以按照老式的方式获取元素数组,并将该数组传递给jQuery。

 function toggleByName() { var arrChkBox = document.getElementsByName("chName"); $(arrChkBox).toggle(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <head> <title>sandBox</title> </head> <body> <input type="radio" name="chName"/><br /> <input type="radio" name="chName"/><br /> <input type="radio" name="chName"/><br /> <input type="radio" name="chName"/><br /> <input type="button" onclick="toggleByName();" value="toggle"/> </body> </html> 

note: the only time you would have a reason to use the "name" attribute should be for checkbox or radio inputs. 注意:您有理由使用“name”属性的唯一一次应该是复选框或无线电输入。

Or you could just add another class to the elements for selection.(This is what I would do) 或者你可以在元素中添加另一个类进行选择。(这就是我要做的)

 function toggleByClass(bolShow) { if (bolShow) { $(".rowToToggle").show(); } else { $(".rowToToggle").hide(); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <head> <title>sandBox</title> </head> <body> <table> <tr> <td>data1</td> <td class="bold rowToToggle">data2</td> </tr> <tr> <td>data1</td> <td class="bold rowToToggle">data2</td> </tr> <tr> <td>data1</td> <td class="bold rowToToggle">data2</td> </tr> </table> <input type="button" onclick="toggleByClass(true);" value="show"/> <input type="button" onclick="toggleByClass(false);" value="hide"/> </body> </html> 

#5楼

Any attribute can be selected using [attribute_name=value] way. 可以使用[attribute_name=value]方式选择任何属性。 See the sample here : 看这里的样本:

var value = $("[name='nameofobject']");

#6楼

If you have something like: 如果你有类似的东西:

<input type="checkbox" name="mycheckbox" value="11" checked="">
<input type="checkbox" name="mycheckbox" value="12">

You can read all like this: 你可以这样读:

jQuery("input[name='mycheckbox']").each(function() {console.log( this.value + ":" + this.checked );
});

The snippet: 片段:

 jQuery("input[name='mycheckbox']").each(function() { console.log( this.value + ":" + this.checked ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" name="mycheckbox" value="11" checked=""> <input type="checkbox" name="mycheckbox" value="12"> 

如何使用jQuery按名称选择元素?相关推荐

  1. jquery按名称选择_jQuery按类别选择

    jquery按名称选择 jQuery select by class name allows us to find a specific HTML element with the value of ...

  2. jquery按名称选择_jQuery按名称选择

    jquery按名称选择 jQuery select by name or jQuery element selector allows us to find all HTML elements wit ...

  3. jQuery learn - 1 - 选择元素 CSS

    2019独角兽企业重金招聘Python工程师标准>>> CDN=Content Delivery Networks DOM=Document Object Model W3C=Wor ...

  4. html name选择器,2种用jQuery按name属性选择元素的方法

    学习2种用jQuery按name属性选择元素的方法,拿走不谢! 给定一个HTML文档,如何使用jQuery按name属性选择元素? 在jQuery中,可以使用2个方法通过name属性选择元素:下面就来 ...

  5. JQuery按name属性选择元素

    方法1:使用name属性选择器 name属性选择器可用于按name属性选择元素.此选择器选择值与指定值完全相等的元素. 语法:[name="nameOfElement"] 示例: ...

  6. fond+html属性,JQuery 干货篇之选择元素

    JQuery 干货篇之选择元素 实验的HTML+CSS的代码 html Example Jacqui's Flower Shop Astor: Daffodil: Rose: Peony: Primu ...

  7. jquery笔记一:下载安装、语法、选择器、遍历选择元素的方法、jQuery动画

    目前 jQuery 兼容于所有主流浏览器, 包括 IE 6!开发时常用 jquery.js,上线用 jquery.min.js. jq插件 目前jQuery有三个大版本: (1)1.x.x: 兼容ie ...

  8. 初识jQuery,八字真言“选择元素,对其操作”

    jQuery在我印象中,就是很多类似$(),然后昨天开始接触了,觉得很和谐,获取元素比JavaScript简单很多,有意思. 一.开始学习jQuery,下载jQuery库的文件 http://jque ...

  9. jQuery选择元素的方法

    一..get(index) 通过jQuery对象获取一个对应的DOM元素. index从0开始,用来确定获取哪个元素. 二.eq(index) 根据索引获取jQuery对象 当index为正数,索引从 ...

最新文章

  1. 1月4日链接篇:ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, IIS7
  2. Matlab图像处理教程
  3. 错误fatal error: curl/curl.h: No such file or directory解决方案
  4. python 语音识别机器人控制系统_python人脸识别+语音识别 的监控系统
  5. 建议11: 区别对待==和Equals
  6. 为了帮助卖家成交,闲鱼工程师做了些什么?
  7. [10] ADB 修改设置
  8. 深度学习2.0-10.tensorflow的高阶操作之张量的限幅
  9. java移位运算_Java 移位运算符
  10. 模COMSOL Multiphysics v5.3 Win64 Linux64 MacOSX64 1DVD
  11. linux下双机热备份系统,基于Linux的双机热备份系统的研究与设计
  12. s1 episode1(一)
  13. 《战争online超极巨兽》故事起源
  14. 苹果手机白屏_为什么安卓用久了会卡顿,苹果用久了会闪退?看完长知识了
  15. linux uwsgi 非root,只能以root身份运行uwsgi
  16. linux下filezilla使用教程,FTP工具filezilla使用教程
  17. 汽车研发的五大阶段及制造的四大工艺
  18. Redis 启动的三种方法
  19. Java中string字符串和char字符之间的千丝万缕
  20. php商城后台管理,商城后台管理系统

热门文章

  1. 找不到或无法加载主类 org.jivesoftware.openfire.starter.ServerStarter
  2. Binder通信模型 :ServiceManager
  3. WindowManager如何被Android深度解析(1)
  4. 多线程编程 之 (生产者与消费者(N多))同步常用的方法。
  5. Eclipse主题更换方法
  6. Ubuntu磁盘分区以及双系统启动顺序修改
  7. 【C/C++】中的__FILE__、__LINE__、#line、__func__关键字(预定义宏)
  8. Java泛型中extends T和super T的区别?
  9. 安装LINUX后要怎么找回WIN7启动项?
  10. python 线性回归 统计检验 p值_SPSS 25 数学统计分析工具