【发布时间】:2018-07-31 19:52:42
【问题描述】:
在动态表格行内部,我有一个选择标签。在通过 Ajax 的第一行上,我可以更改选择标记值,但在动态第二行选择标记中,如果我进行任何更改,它会反映在第一行选择标记上,而不是在同一行上。
我在这里做错了什么?
$(document.body).on('change', '#carwash', function() {
var washid = $(this).val();
console.log(washid);
if (washid) {
$.ajax({
type: 'POST',
url: '<?php echo base_url();?>/Main/fetch_items',
data: 'washid=' + washid,
dataType: "html",
success: function(response) {
console.log(response);
$('#product').html(response);
}
});
}
});
$(document.body).on('click', '.product', function() {
var product = $(this).val();
console.log(product);
$.ajax({
type: 'POST',
url: '<?php echo base_url();?>/Main/fetch_mesure',
data: 'subid=' + product,
dataType: "html",
success: function(test) {
console.log(test);
$('#unit').val(test);
}
});
$.ajax({
type: 'POST',
url: '<?php echo base_url();?>/Main/fetch_limit',
data: 'subid=' + product,
dataType: "html",
success: function(test) {
console.log(test);
$('#quantity').val(test);
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl_posts" class="table table-bordered table-hover tbl">
<thead>
<tr>
<td>Wash type</td>
<td>Product </td>
<td>Unit</td>
<td>One wash requirment</td>
<td>Action</td>
</tr>
</thead>
<tbody id="tbl_posts_body">
<tr id="rec-1">
<td>
<select class="form-control custom-select" name='washtype[]' required id='carwash'>
<option value="">Choose washtype</option>
?php foreach($washtype as $row): ?>
<option value="<?php echo $row->id; ?>">
<?php echo $row->service_name; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<select class="form-control custom-select product" name='product[]' required id='product'>
<option value="">Choose Product</option>
<?php foreach($itemlist as $row): ?>
<option value="<?php echo $row->id; ?>">
<?php echo $row->item_name; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<input type="text" class="form-control" placeholder="wash limit" id="unit" name='unit[]' readonly>
</td>
<td>
<input type="text" class="form-control" placeholder="wash limit" id='quantity' name='quantity[]' readonly>
</td>
<td>
<div class="form-group has-success">
<a class="btn btn-success btn-rounded pull-right add-record" data-added="0"> Add Row</a>
</div>
</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
1.如果需要 ID,则需要 UNIQUE ID 2. 如果使用相对寻址,则不需要 ID:
var washtype = $(this).closest("tr").find("[name='washtype[]']")