【发布时间】:2022-01-04 12:07:17
【问题描述】:
我正在使用 javascript 函数输入一个数字并在 boostrap 模态中计算后自动显示结果。
如果单击第一行的请求按钮,javascript 函数可以正常工作。但是当我点击第二行和后续行的请求按钮时,它不起作用。
我不确定问题是模态内部的数据还是模态外部的数据
我尝试使用 getElementsByClassName 而不是 ID,但它仍然无法正常工作。
<script>
function mult(value) { // === calculate the profit and the total amount due === //
var x = value * 15 / 100;
var y = parseFloat(x)+parseInt(value);
document.getElementById("profit").value = x;
document.getElementById("total").value = y;
}
</script>
<table id="table1">
<thead>
<tr>
<th>#</th>
<th>Full Name</th>
<th>Requests</th>
</tr>
</thead>
<tbody>
@foreach($Clients as $Client)
<tr id="foreach-row">
<td>{{ $Client->full_name }}</td>
<td>
<div class="buttons">
<a class="btn icon btn-secondary" data-toggle="modal"
data-target="#LoanRequestModel-{{ $Client->id }}">
Request
</a>
</div>
</td>
</tr>
{{-- =========== MODEL - NEW REQUEST ===============--}}
<div class="modal" id="LoanRequestModel-{{ $Client->id }}" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><b>REQUEST</b></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<i data-feather="x"></i>
</button>
</div>
<div class="modal-body">
<form action="{{ route('finance.storeRequest', $Client->id) }}" method="post"
class="loanrequest_form" enctype="multipart/form-data">
{{ csrf_field() }}
<label>Full name</label>
<div class="form-group">
<input type="text" name="creditor_full_name"
class="form-control" value="{{ $Client->full_name }}" >
</div>
<div>
<label>Amount</label>
<div class="form-group">
<input type="number" name="loan_amount"
class="form-control" onkeyup="mult(this.value);" >
</div>
</div>
<div class="row">
<div class="col-md-5">
<label>Interest Rate</label>
<div class="input-group mb-3">
<label class="input-group-text" for="inputGroupSelect01">Choose</label>
<select class="form-select rate"name="interest_rate" >
<option value="15" selected>15%</option>
<option value="20">20%</option>
</select>
</div>
</div>
<div class="col-md-7">
<label>Profit</label>
<div class="input-group mb-3">
<input type="text" id="profit" class="form-control" name="profit"
aria-label="Amount">
</div>
</div>
</div>
<div class="row">
<label>Total Amount due</label>
<div class="input-group mb-3">
<input type="text" id="total" class="form-control" name="amount_due"
aria-label="Amount">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-outline-secondary ml-1" >
<i class="bx bx-check d-block d-sm-none"></i>
<span class="d-none d-sm-block">Submit</span>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endforeach
</tbody>
</table>
【问题讨论】:
-
ID 必须是唯一的
-
它真的“不起作用”吗?请在您的 mult 函数中使用
console.log('foo')进行检查。我怀疑它确实起作用,但它正在改变相同的元素。如果是这种情况,请在您的 mult 函数中传递对元素的引用,而不是使用类和 id -
<div>不是<tbody>的有效子代。那个地方只允许<tr>s。 -
@Andreas 我必须让哪个 id 独一无二?该脚本仅适用于 foreach 循环的第一项,无法处理后续行。
-
与“Ids have to be unique”,指的是html标签的属性。请阅读此The id global attribute defines an identifier (ID) which must be unique in the whole document.
标签: javascript html laravel bootstrap-modal