我有這樣的代碼在我看來:如何通過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