javascript - laravel 验证,如何在客户端添加验证规则?
问题描述
//html code
<form>
<input type="radio" id="defult" name="price_type" value="default">
<label for="defult">Default Price</label><br>
<input type="radio" id="custom" name="price_type" value="custom">
<label for="custom">Custom Price</label><be>
<input placeholder="Custom Price" class="form-control" name="custom_price">
</form>
$('input[type=radio][name=price_type]').change(function() {
if (this.value == 'default') {
//make custom_price optional
}
else if (this.value == 'custom') {
//make custom_price required
}
});
实际上,我有一个用于自定义价格或默认价格的单选框,如果用户选择自定义价格,则希望 input[name=custom_price]必需,或者如果用户选择默认价格,则使 input[name=custom_price]可选。
解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form>
<input type="radio" id="default" name="price_type" value="default">
<label for="default">Default Price</label><br>
<input type="radio" id="custom" name="price_type" value="custom">
<label for="custom">Custom Price</label><br>
<input placeholder="Custom Price" class="form-control" name="custom_price" required>
<input type="submit" value="Submit">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('input[type=radio][name=price_type]').change(function() {
if (this.value == 'default') {
//make custom_price optional
$('input[name=custom_price]').prop('required', false);
}
else if (this.value == 'custom') {
//make custom_price required
$('input[name=custom_price]').prop('required', true);
}
});
</script>
</body>
</html>
推荐阅读
- python-3.x - Python 3 - 作为字典导入的文件,但值是列表 - 如何解决?
- ruby-on-rails - 在“新”视图上显示验证错误
- c++ - 是否允许 extern extern "C" 和 extern "C" extern?
- python - 从给定的日期列表中获取该月的最后一个日期
- c# - MVC 4项目IIS执行超时不起作用
- javascript - 如何判断具有速度的位置是向内还是向外的矩形?
- python - Conda 安装和更新不起作用也解决了环境错误
- javascript - 一种形式到另一种形式的总和
- typescript - 在 AWS CDK 中执行代码之前如何等待堆栈完成?
- r - 如何为某个范围创建嵌套的for循环