jQuery等同于“getElementsByName”;

jQuery等同于“getElementsByName”;

问题描述:

getElementsByName 调用的正确jquery语法是什么?

What is the correct jquery syntax for a getElementsByName call?

这是我的javascript代码:

Here is my javascript code:

var test = document.getElementsByName(tableName)[0];

使用此值返回不同的值:

using this is returning a different value:

var test = $("[name=tableName]");

提前致谢

属性选择器周围使用引号:

$('[name="somenamehere"]');

如果需要在选择器中使用变量,则需要使用字符串连接来获取值变量:

If you need to use a variable within a selector, you need to use string concatenation to get the value of the variable:

$('[name="' + tableName + '"]');






通常应避免使用 [name] 属性支持 [id] 属性,因为选择会更简单:


Typically one should avoid using the [name] attribute in favor of the [id] attribute, because selection would be simpler as:

$('#someidhere');
-or-
$('#' + tableID);