我有一个Laravel Blade表单,在其中填写所需的字段,如下所示:
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
这有效,当我尝试提交没有name
值的表单时,出现此错误:
但是,现在我有一个单选按钮,另一个字段是postal_code
:
<input type="radio" name="billing_type" class="billing_type" value="paypal" checked /> PayPal
<input type="radio" name="billing_type" class="billing_type" value="card" /> Credit Card
<input type="text" class="form-control" name="postal_code" value="{{ old('postal_code') }}" placeholder="" />
postal_code
仅当单选按钮设置为value时,如何使该字段为必填字段card
?
我可以在后端处理验证,但是我想在刀片服务器上进行前端验证,就像name
上面的字段一样。
您可以使用jquery进行添加,如果值是card,则添加一个必需的属性。否则,将其删除。
$('.billing_type').change(function() {
var type = $(this).val();
if(type == 'card'){
$('.postal_code').attr("required","required");
}else{
$('.postal_code').removeAttr("required");
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句