选择同名元素时出现JQuery语法错误

选择同名元素时出现JQuery语法错误,jquery,Jquery,我想选择具有相同名称的输入,但出现语法错误: $("input[name=$(this).prop('name')]"); 但是我可以通过$(this.prop('name')获得字符串。 编辑: 谢谢你的回答。我想它们都有用。但有人能解释为什么开头和结尾的两个加号使jquery代码在字符串中解析吗?参考将不胜感激,谢谢 EDIT2: 我知道加号表示字符串连接。但在本例中,没有任何内容在第一个+之前或第二个+之后。那么这里连接的是什么?抱歉,如果问题看起来太幼稚了应该是这样的: $("i

我想选择具有相同名称的输入,但出现语法错误:

$("input[name=$(this).prop('name')]"); 
但是我可以通过
$(this.prop('name')
获得字符串。

编辑:

谢谢你的回答。我想它们都有用。但有人能解释为什么开头和结尾的两个加号使jquery代码在字符串中解析吗?参考将不胜感激,谢谢

EDIT2:


我知道加号表示字符串连接。但在本例中,没有任何内容在第一个+之前或第二个+之后。那么这里连接的是什么?抱歉,如果问题看起来太幼稚了

应该是这样的:

$("input[name='"+$(this).prop('name')+"']"); 
jQuery不解析字符串上的jQuery代码。要执行它,您应该将jQuery
prop()
函数放在字符串外部。

它应该是这样的:

$("input[name='"+$(this).prop('name')+"']"); 

jQuery不解析字符串上的jQuery代码。要执行它,您应该将jQuery
prop()
函数置于字符串之外。

您实际上是在寻找一个名为
$(this.prop('name')
输入
元素。你要做的是写这样的东西:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />

您实际上是在寻找名为
$(this.prop('name')
)的
输入元素。你要做的是写这样的东西:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />

试着这样做:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />

试着这样做:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />
但有人能解释为什么开头和结尾的两个加号使jquery代码在字符串中解析吗

让我们一步一步来看看

首先,让我们采用原始方法:

$("input[name=$(this).prop('name')]"); 
函数“$”(jQuery的别名)将使用字符串参数调用:

"input[name=$(this).prop('name')]"
jQuery将解析此字符串并如下解释:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />
您正在从JavaScript对象构造字符串的一部分。使用$(this)(这是一个jQuery对象),调用其函数“prop”来检索名为“name”的属性,并在新查询中使用该函数的输出。因此,如果$(this.prop('name')返回“AWESOME”,则要求jQuery查找如下内容:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />

这应该更像你要找的

但有人能解释为什么开头和结尾的两个加号使jquery代码在字符串中解析吗

让我们一步一步来看看

首先,让我们采用原始方法:

$("input[name=$(this).prop('name')]"); 
函数“$”(jQuery的别名)将使用字符串参数调用:

"input[name=$(this).prop('name')]"
jQuery将解析此字符串并如下解释:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />
您正在从JavaScript对象构造字符串的一部分。使用$(this)(这是一个jQuery对象),调用其函数“prop”来检索名为“name”的属性,并在新查询中使用该函数的输出。因此,如果$(this.prop('name')返回“AWESOME”,则要求jQuery查找如下内容:

$("input[name="+$(this).prop('name')+"]"); 
$("input[name=" + $(this).prop('name') + "]");
/* 
Okay, I need to find an <input> 
that has an attribute "name", 
and the value of that attribute has to be, literally, "$(this).prop('name')". 
So, I'm looking for something that looks like this: */

<input name="$(this).prop('name')" />
<input name="some name, actually" />
<input name="AWESOME" />


这应该更像你要找的

名称值周围是否缺少单引号?也许我错了,你能解释一下为什么开头和结尾的两个加号使jquery代码在字符串中被解析吗?请参考,谢谢。您是否缺少名称值周围的单引号?也许我错了,你能解释一下为什么开头和结尾的两个加号使jquery代码在字符串中被解析吗?敬请参考,谢谢您的详细介绍。我完全听懂你说的每一个字。但我的问题仍然是:为什么这两个加号可以做他们所做的?换句话说,为什么这两个加号使jquery解析变量位于字符串中?这是一种特殊的语法还是什么?我不能在谷歌上搜索任何关于这个的参考。我知道加号表示字符串连接。但在本例中,没有任何内容在第一个+之前或第二个+之后。这让我感到困惑。它没有什么特别之处,这是非常基本的JavaScript。在上面的代码中,
$(this).prop('name')
被执行并返回“AWESOME”,它将为您提供
$(“input[name=AWESOME]”)
。注意字符串concatation的引号前后的双精度(“),但是我们在这里没有连接任何东西……没有东西在first+之前或second+之后。我们连接的字符串是:“input[name]”(这是一个)、调用prop('name')(这是两个)的结果和“]“-那是三。三根弦被扭断了。谢谢你的详细说明。我完全听懂你说的每一个字。但我的问题仍然是:为什么这两个加号可以做他们所做的?换句话说,为什么这两个加号使jquery解析变量位于字符串中?这是一种特殊的语法还是什么?我不能在谷歌上搜索任何关于这个的参考。我知道加号表示字符串连接。但在本例中,没有任何内容在第一个+之前或第二个+之后。这让我感到困惑。它没有什么特别之处,这是非常基本的JavaScript。在上面的代码中,
$(this).prop('name')
被执行并返回“AWESOME”,它将为您提供
$(“input[name=AWESOME]”)
。注意字符串连接的引号前后的双精度(“)。但是我们在这里没有连接任何东西…没有任何东西在first+或second+之前。我们连接的字符串是:“input[name]”(那是一个),调用prop('name')(那是两个)的结果,以及“]”-那是三个。三个字符串被连接。