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

T.T*_*.T. 1160 html javascript jquery dom jquery-selectors

有一个表格列我正在尝试展开和隐藏:

td当我按而不是按元素名称选择它时,jQuery似乎隐藏了元素.

例如,为什么:

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

请注意下面的HTML,第二列对所有行都具有相同的名称.我怎么能用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>
Run Code Online (Sandbox Code Playgroud)

Jon*_*son 2075

您可以使用属性选择器:

$('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'
Run Code Online (Sandbox Code Playgroud)

  • @Varun - 你可以省略td ...例如$('[name ^ = tcol]')将匹配所有具有属性'name'的元素,其值以'tcol'开头 (20认同)
  • @HPWD你会使用:not selector,`$("td:not([name ='tcol1'])")`例如.你可以在[这个小提琴](https://jsfiddle.net/4v83g21u/1/)看到它 (5认同)
  • @Jon Erickson - 如果我不知道元素标签(在这种情况下是td),那该怎么办? (3认同)

小智 211

可以使用[attribute_name=value]方式选择任何属性.看这里的样本:

var value = $("[name='nameofobject']");
Run Code Online (Sandbox Code Playgroud)

  • 这至少对我来说不起作用 - 但是下面的语句起作用`var value = $("[name = nameofobject]");` (7认同)
  • 21 upvotes now ...也许是因为答案比'td [name = tcol1]'更容易混淆,特别是因为td不是必需的,因此导致像我这样的人走错了路 (6认同)
  • 令人惊讶的是,在原来接受的答案发布4年后,这次获得了17次赞成 (2认同)
  • 我的输入名称具有一个[],如下所示:`&lt;input name =“ itemid []”&gt;`。因此,由于正确使用了引号,因此此答案比接受的答案更完美。 (2认同)

And*_* L. 57

如果你有类似的东西:

<input type="checkbox" name="mycheckbox" value="11" checked="">
<input type="checkbox" name="mycheckbox" value="12">
Run Code Online (Sandbox Code Playgroud)

你可以这样读:

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

片段:

jQuery("input[name='mycheckbox']").each(function() {
  console.log( this.value + ":" + this.checked );
});
Run Code Online (Sandbox Code Playgroud)
<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">
Run Code Online (Sandbox Code Playgroud)


You*_*Ken 25

你可以按照老式的方式获取元素数组,并将该数组传递给jQuery.

function toggleByName() {
  var arrChkBox = document.getElementsByName("chName");
  $(arrChkBox).toggle();
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

注意:您有理由使用"name"属性的唯一一次应该是复选框或无线电输入.

或者你可以在元素中添加另一个类进行选择.(这就是我要做的)

function toggleByClass(bolShow) {
  if (bolShow) {
    $(".rowToToggle").show();
  } else {
    $(".rowToToggle").hide();
  }
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Shr*_*t D 23

您可以使用jQuery中的name元素从输入字段获取名称值:

var firstname = jQuery("#form1 input[name=firstname]").val(); //Returns ABCD
var lastname = jQuery("#form1 input[name=lastname]").val(); //Returns XYZ 
console.log(firstname);
console.log(lastname);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="form1" id="form1">
  <input type="text" name="firstname" value="ABCD"/>
  <input type="text" name="lastname" value="XYZ"/>
</form>
Run Code Online (Sandbox Code Playgroud)


its*_*lay 15

框架通常在表单中使用括号名称,例如:

<input name=user[first_name] />
Run Code Online (Sandbox Code Playgroud)

可通过以下方式访问它们:

// in JS:
this.querySelectorAll('[name="user[first_name]"]')

// in jQuery:
$('[name="user[first_name]"]')

// or by mask with escaped quotes:
this.querySelectorAll("[name*=\"[first_name]\"]")
Run Code Online (Sandbox Code Playgroud)


ksc*_*ius 13

我这样做了,它的工作原理:

$('[name="tcol1"]')
Run Code Online (Sandbox Code Playgroud)

https://api.jquery.com/attribute-equals-selector/

  • 字面上给出了上面给出的答案:http://stackoverflow.com/a/18947703/439094 (2认同)

小智 5

您可以使用任何属性作为选择器[attribute_name=value]

$('td[name=tcol1]').hide();
Run Code Online (Sandbox Code Playgroud)


小智 5

这是一个简单的解决方案: $('td[name=tcol1]')


Chr*_*s J 5

您忘记了第二组引号,这会使接受的答案不正确:

$('td[name="tcol1"]') 
Run Code Online (Sandbox Code Playgroud)