Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

php - Get old value from dynamic input in Laravel with JavaScript?

I want to get the old value from dynamic input on Laravel. If it's a regular input, I only need {{ old ('name') }}. But what if the input is dynamic?

View

<tr>
    <td>
        <input type="text" name="name_progress[]"
               value="{{ old('name_progress[]') }}"
               placeholder="Enter name" class="form-control"/>
    </td>
    <td>
        <input type="text" name="payment_percentage[]"
               value="{{ old('payment_percentage[]') }}"
               placeholder="Enter % invoice" class="form-control"/>
    </td>
    <td>
        <a type="button" class="btn btn-danger remove-tr">-</a>
    </td>
</tr>

JavaScript

<script type="text/javascript">
    $("#add-btn").click(function () {
        var tr = '<tr>' +
            '<td>' +
            '<input type="text" name="name_progress[]" ' +
            'placeholder="Enter name" class="form-control" /></td>' +
            '<td><input type = "text" name = "payment_percentage[]" ' +
            'placeholder = "Enter per_invoice" class = "form-control" />' +
            '</td>' +
            '<td><a type="button" class="btn btn-danger remove-tr">-</a></td>' +
            '</tr>';
        $("#dynamicProgress").append(tr);
    });
    $(document).on('click', '.remove-tr', function () {
        $(this).parents('tr').remove();
    });
</script>
question from:https://stackoverflow.com/questions/65560165/get-old-value-from-dynamic-input-in-laravel-with-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since name_progress and payment_percentage fields are arrays, you may simply loop through the old input values.

@forelse (old('name_progress', []) as $i => $name_progress)
    <tr>
        <td>
            <input type="text" name="name_progress[]" value="{{ $name_progress }}" placeholder="Enter name" class="form-control" />
        </td>
        <td>
            <input type="text" name="payment_percentage[]" value="{{ old('payment_percentage')[$i] }}" placeholder="Enter % invoice" class="form-control" />
        </td>
        <td>
            <a type="button" class="btn btn-danger remove-tr">-</a>
        </td>
    </tr>
@empty
    <tr>
        <td>
            <input type="text" name="name_progress[]" placeholder="Enter name" class="form-control" />
        </td>
        <td>
            <input type="text" name="payment_percentage[]" placeholder="Enter % invoice" class="form-control" />
        </td>
        <td>
            <a type="button" class="btn btn-danger remove-tr">-</a>
        </td>
    </tr>
@endforelse

If old('name_progress') is empty or null, initial fields with empty values will be displayed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...