2016-12-07 80 views
1

我有一个脚本允许我将值粘贴到不同的输入中。在jQuery中按名称[]过滤输入

<input name="BOO_Price"> 
<a id="repeatPrice">Repeat</a> 
<input name="BOO_Price"> 
<input name="BOO_Price"> 
<input name="BOO_Price"> 
<input name="BOO_Price"> 


$('#repeatPrice').on('click', function(e) { 
    e.preventDefault(); 
    var price = $(this).prev('input').val(); 
    $('input[name=BOO_Price]').val(price); 
}); 

它完美地工作。

但现在我的输入形式是这样的。

<input name="BOO_Price[]"> 

但是,这是行不通的:

$('input[name=BOO_Price[]]').val(price); 

理想情况下,我不希望有一些id或其他形式的classes


你能帮忙吗?

回答

1

由于值包含元字符,因此用引号将属性值换行。

$('input[name="BOO_Price[]"]').val(price); 

或逃生使用\\的元字符。

$('input[name=BOO_Price\\[\\]]').val(price);