如何使用jQuery按名称选择元素?

本文翻译自: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"> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery选择器可以分为基本选择器、层次选择器、过滤选择器、内容选择器和属性选择器。具体如下: 1. 基本选择器 - `*`:匹配所有元素。 - `element`:根据元素名称匹配元素。 - `.class`:根据class名称匹配元素。 - `#id`:根据ID名称匹配元素。 - `selector1,selector2,selectorN`:匹配任意一个选择器。 2. 层次选择器 - `ancestor descendant`:匹配所有符合条件的后代元素。 - `parent > child`:匹配所有符合条件的直接子元素。 - `prev + next`:匹配紧接在prev元素后面的next元素。 - `prev ~ siblings`:匹配prev元素之后的所有siblings元素。 3. 过滤选择器 - `:first`:匹配第一个元素。 - `:last`:匹配最后一个元素。 - `:even`:匹配偶数位置的元素。 - `:odd`:匹配奇数位置的元素。 - `:eq(index)`:匹配指定位置的元素。 - `:gt(index)`:匹配大于指定位置的元素。 - `:lt(index)`:匹配小于指定位置的元素。 - `:not(selector)`:匹配不符合选择器条件的所有元素。 4. 内容选择器 - `:contains(text)`:匹配包含指定文本的元素。 - `:empty`:匹配不包含任何子元素或文本的空元素。 - `:has(selector)`:匹配拥有符合条件的后代元素元素。 - `:parent`:匹配至少包含一个子元素或文本的元素。 5. 属性选择器 - `[attribute]`:匹配具有指定属性的元素。 - `[attribute=value]`:匹配属性值完全等于指定值的元素。 - `[attribute^=value]`:匹配属性值以指定值开头的元素。 - `[attribute$=value]`:匹配属性值以指定值结尾的元素。 - `[attribute*=value]`:匹配属性值包含指定值的元素。 - `[attribute|value]`:匹配属性值等于指定值或以指定值开头并且后面跟一个连字符的元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值