jquery 将字段值附加到按钮数据属性

kuarbcqp  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(122)

我在blade中有几个fieldsets,每个字段集都有相同的inputselect字段和一个保存按钮

<div class="unit_box">
<input class="form-control {{ $errors->has('target_value') ? 'is-invalid' : '' }}" type="text" name="target_value" id="target_value" placeholder="{{ trans('cruds.validationRule.fields.target_value') }}" value="{{ old('target_value', '') }}">

<select class="form-control {{ $errors->has('unit') ? 'is-invalid' : '' }}" name="unit" id="unit" required>
    <option value disabled {{ old('unit', null) === null ? 'selected' : '' }}>{{ trans('global.pleaseSelect') }}</option>
    @foreach(App\Models\ValidationRule::UNIT_SELECT as $key => $label)
    <option value="{{ $key }}" {{ old('unit', 'g') === (string) $key ? 'selected' : '' }}>{{ $label }}</option>
    @endforeach
</select>
<button class="next action-button" type="button" name="next" data-gotostep="3" data-unit="" data-value="">Save</button>

如何发送输入字段target_value的值和按钮属性data-unit="" data-value=""unit的值,以便在jQuery函数中使用它们?

3pvhb19x

3pvhb19x1#

你必须学习jQuery的基础知识,它很简单,有很多你想要实现的例子,你可以这样做:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script>
$(document).ready(function(){
    // Listen for changes in the input field
    $("#target_value").on("input", function() {
        let value = $(this).val();
        $(".next").attr("data-value", value);
    });

    // Listen for changes in the select dropdown
    $("#unit").on("change", function() {
        let unit = $(this).val();
        $(".next").attr("data-unit", unit);
    });
});
</script>

相关问题