我有这样的代码在我看来:如何通过jquery更改部分输入的名称?
<div class="box">
<input type="text" name="product[size_ids][<%= size.id %>][quantity][1]" readonly class="product_quantity" placeholder="quantity from" value="1">
</div>
在我的js我想改变[1]
到[2]
或[3]
等后[quantity]
,这取决于有多少其他形式的创建。我怎样才能做到这一点?
这是我在我的JS:
var i = 1
$('.add_another_price_btn').click(function (e) {
e.preventDefault();
$(this).prev().clone().insertBefore($(this));
$(this).prev().find('.remove_another_price_btn').show();
$(this).prev().find('.product_quantity').removeAttr('readonly');
$(this).prev().find('.product_quantity').attr('value', '');
//This is what I tried, but it doesn't work properly.
$(this).prev().find('.product_quantity')
.attr('name', function() { return $(this).attr('name') + '['+ (i++) + ']' });
$('.remove_another_price_btn').click(function (ee) {
ee.preventDefault();
$(this).parent().parent().remove();
});
定期EXPR激情 – epascarello
@VijayMaheriya他做到了。 – mplungjan