【问题标题】:Setting the minimum value of field based on another field in Laravel根据 Laravel 中的另一个字段设置字段的最小值
【发布时间】:2021-12-21 05:51:02
【问题描述】:
我的表单中有一个start 和stop 字段,我希望用户输入一个start 字段然后输入stop 字段,但是,stop 字段的最小值需要成为start 字段的值。有什么方法可以实现吗?
我的看法:
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" style="width: 7em"class="col-form-label">Start</label>
<input type="number"style="width: 7em" name="start" class="form-control" id="start" min="0" required>
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" style="width: 7em"class="col-form-label">Stop</label>
<input type="number"style="width: 7em" name="stop" class="form-control" id="stop" min="0" required>
</div>
【问题讨论】:
标签:
laravel
laravel-8
laravel-blade
【解决方案1】:
使用onchange 处理程序解决了这个问题:
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" style="width: 7em"class="col-form-label">Start</label>
<input type="number"style="width: 7em" name="start" class="form-control" id="start" min="0" onchange="document.getElementById('stop').min=this.value;" required>
</div>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">End ODO</label>
<input type="number" style="width: 7em" name="stop" class="form-control @error('stop') is-invalid @enderror" id="stop" min="document.getElementById('start').value" value="{{ old('stop') }}" required>
@error('end_odo')
<span class="invalid-feedback">{{ $message }}</span>
@enderror
</div>
【解决方案2】:
使用这个简单的 jQuery 方法:
Add `disabled` attribute in your stop input Html
<input type="number"style="width: 7em" name="stop" class="form-control"
disabled id="stop" min="0" required>
然后使用jQuery:
$(document).on('blur','#start',function(){
//make stop input disabled false after focus out from start input
$("#stop").prop("disabled",false);
//set stop input minimum value to start value
$("#stop").attr("min",$("#start").val());
$("#stop").val($("#start").val());
});