Javascript 函数仅适用于我的 foreach laravel 循环中的第一个 table 行

Javascript function works only on the first table row inside my foreach laravel loop

我正在使用 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 唯一时:

id="LoanRequestModel-{{ $Client->id }}"

在您的循环中,当生成每个模态时,您使用的字段具有非唯一 ID:

type="text" id="profit" 
type="text" id="total"

最好让这些 ID 不唯一,就像您对模态 ID 所做的那样:

type="text" id="profit-{{ $Client->id }}"
type="text" id="total-{{ $Client->id }}"

添加 ID 到您需要在您的脚本中使用但目前没有的字段 :

type="number" id="loan_amount-{{ $Client->id}}" name="loan_amount"

然后,当您在此处触发 javascript on keyup 时:

onkeyup="mult(this.value);"

不是传递字段的值,而是传递客户端 ID:

onkeyup="mult({{ $Client->id);"

然后你只需要调整你的javascript(因为它不再被传递loan_amount)所以它首先得到loan_amount,然后是其他零碎的东西需要,终于returns他们到对地方了:

<script>
    function mult(id) { // === this is now receiving the ID, not a value so first we need to get the value for the relevant field *emphasized text*== //
        var value = document.getElementById("loan_amount-" + id).value
        var x = value * 15 / 100;
        var y = parseFloat(x)+parseInt(value);
        document.getElementById("profit" + id).value = x;
        document.getElementById("total" + id).value = y;
    }
</script>