将需要检查相应的复选框所需的输入字段
我正在制作一种表格,其中我只从复选框组中选择一个复选框。 我想检查所需的输入字段,该字段已检查相应的复选框。
$(document).ready(function() {
$('.check').click(function() {
let name = $(this).attr("name")
if ($(this).prop('checked')) {
$('.check[name="' + name + '"]').prop('required', false)
} else {
$('.check[name="' + name + '"]').prop('required', true)
}
$('.check[name="' + name + '"]').not(this).prop('checked', false)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>A. Passport Number</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>B. Voter ID Card</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>C. Driving Licence</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>D. NREGA Job Card</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5 class="d-inline">E. National Population Register Letter</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5 class="d-inline">F. Proof of Possession on Aadhaar</h5>
<input type="text">
在这里,我只能选择一个复选框,然后进行相应的文本字段。 我想要如果选择第n个复选框,则必须填写第n个输入字段。
I am making a form where I am selecting only one checkbox from group of checkbox.
I want to make input field required whose corresponding checkbox are checked.
$(document).ready(function() {
$('.check').click(function() {
let name = $(this).attr("name")
if ($(this).prop('checked')) {
$('.check[name="' + name + '"]').prop('required', false)
} else {
$('.check[name="' + name + '"]').prop('required', true)
}
$('.check[name="' + name + '"]').not(this).prop('checked', false)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>A. Passport Number</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>B. Voter ID Card</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>C. Driving Licence</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5>D. NREGA Job Card</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5 class="d-inline">E. National Population Register Letter</h5>
<input type="text"><br>
<input type="checkbox" id="OVD" name="OVD" class="check" required>
<h5 class="d-inline">F. Proof of Possession on Aadhaar</h5>
<input type="text">
here I can only select one checkbox and make its corresponding text field required.
I want if I select nth checkbox then nth input field will mandatory to be filled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将输入链接到相应的Checkboox。将相应的控件分组为
div
。这使查询相关输入变得更加容易。例如:
You'll want to 'link' the input to the corresponding checkboox. Group the corresponding controls in a
div
. This makes it easier to query the related inputs.For example: