Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用正则表达式按名称选择元素_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 如何使用正则表达式按名称选择元素

Javascript 如何使用正则表达式按名称选择元素,javascript,jquery,regex,Javascript,Jquery,Regex,我有一个带有一些记录的表格,在每一行tr我有两个TD中的两个文本框, 所有文本框都没有Id和Class,它们只有一个名称,它们的名称如下所示 PurchaseDetails[some number].Quantity PurchaseDetails[some number].PurchasePrice 比如: 我使用以下代码,但不起作用: var ProductQuantity = $(this).find("input[name=PurchaseDetails[/^[0-9]+$/].Qua

我有一个带有一些记录的表格,在每一行
tr
我有两个
TD
中的两个文本框, 所有文本框都没有
Id
Class
,它们只有一个
名称
,它们的名称如下所示

PurchaseDetails[some number].Quantity
PurchaseDetails[some number].PurchasePrice
比如:

我使用以下代码,但不起作用:

var ProductQuantity = $(this).find("input[name=PurchaseDetails[/^[0-9]+$/].Quantity]").val();
var ProductPurchase = $(this).find("input[name=PurchaseDetails[/^[0-9]+$/].PurchasePrice]").val();
我的完整html代码是:

 <tr >                                                        
 <td><input type="text" class="form-control" name="PurchaseDetails[1457161853893].Quantity" ></td>
 <td><input type="text" class="form-control" name="PurchaseDetails[1457161853893].PurchasePrice" ></td>
 </tr>

如果当前上下文中只有一个元素具有该前缀和后缀(
$(this)
),则可以使用

$(this)
    .find('input[name^="PurchaseDetails"][name$="Quantity"]').val();

如果当前上下文中只有一个元素具有该前缀和后缀(
$(this)
),则可以使用

$(this)
    .find('input[name^="PurchaseDetails"][name$="Quantity"]').val();
您可以使用使用正则表达式进行过滤

//将`$('input')`替换为`$(this)。查找('input')`以避免在全局上下文中搜索
var ProductQuantity=$(“输入”).filter(函数(){
return/^PurchaseDetails\[\d+\]\.Quantity$/.test(this.name);
}).val();
var ProductPurchasePrice=$(“输入”).filter(函数(){
返回/^PurchaseDetails\[\d+\]\.PurchasePrice$/.test(this.name);
}).val();
console.log(ProductQuantity、ProductPurchasePrice)

您可以使用使用正则表达式进行过滤

//将`$('input')`替换为`$(this)。查找('input')`以避免在全局上下文中搜索
var ProductQuantity=$(“输入”).filter(函数(){
return/^PurchaseDetails\[\d+\]\.Quantity$/.test(this.name);
}).val();
var ProductPurchasePrice=$(“输入”).filter(函数(){
返回/^PurchaseDetails\[\d+\]\.PurchasePrice$/.test(this.name);
}).val();
console.log(ProductQuantity、ProductPurchasePrice)


要使用
过滤器()
请参阅使用
过滤器()
请参阅@Tushar。这不是完整的代码。也许,制作现场演示。好的,我很抱歉。等等。@t请看:[1]:@Tushar请看更新。这不是完整的代码。也许,制作现场演示。好的,我很抱歉。等等。@t请看:[1]: