Saya mengalami kesulitan dalam melakukan pencarian dan penggantian nilai atribut.
Pertimbangkan html ini:
<tr id="rules[0]">
<td>
<input id="rules0.isActive1" name="rules[0].isActive" type="checkbox" value="true"/><input type="hidden" name="_rules[0].isActive" value="on"/>
</td>
<td>
<select id="rules0.leftCondition.name" name="rules[0].leftCondition.name">
...
</select>
</td>
Saya ingin memperbarui setiap atribut nama
Saya menggunakan cuplikan JQuery ini:
$(newRow).find('[id*="rules"]').each(function(){
// Update the 'rules[0]' part of the name attribute to contain the latest count
this.name.replace('rules\[0\]','rules\[$count\]');
});
di mana newRow adalah variabel yang menampung seluruh blok
Di mana kesalahan saya di sini?
Terima kasih
Untuk memperbarui atribut elemen, Anda harus menggunakan fungsi .attr. Selain itu, ketika menggunakan ini dalam konteks jQuery Anda harus menggunakan sintaks $(this). Jika saya memahami maksud Anda, maka seharusnya seperti itu:
$(newRow).find('[id*="rules"]').each(function(){
// Update the 'rules[0]' part of the name attribute to contain the latest count
$(this).attr('name',$(this).attr('name').replace('rules\[0\]','rules\[$count\]'));
});