将需要检查相应的复选框所需的输入字段

发布于 2025-02-08 19:17:05 字数 1784 浏览 1 评论 0原文

我正在制作一种表格,其中我只从复选框组中选择一个复选框。 我想检查所需的输入字段,该字段已检查相应的复选框。

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2025-02-15 19:17:05

您需要将输入链接到相应的Checkboox。将相应的控件分组为div。这使查询相关输入变得更加容易。

例如:

$(document).ready(function() {
    $('.check').click(function() {
        let name = $(this).attr("name")
        let text = $(this).siblings('input[type=text]').first()

        if ($(this).prop('checked')) {
            $('.check[name="' + name + '"]').prop('required', true)
            text.prop('required', true)
        } else {
            $('.check[name="' + name + '"]').prop('required', false)
            text.prop('required', false)
        }
        
        $('.check[name="' + name + '"]').not(this).prop('checked', false)
        $('.text').not(text).prop('required', 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>

    <div>
        <h5>A. Passport Number</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>B. Voter ID Card</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>C. Driving Licence</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>D. NREGA Job Card</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5 class="d-inline">E. National Population Register Letter</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5 class="d-inline">F. Proof of Possession on Aadhaar</h5>
        <input type="text">
    </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:

$(document).ready(function() {
    $('.check').click(function() {
        let name = $(this).attr("name")
        let text = $(this).siblings('input[type=text]').first()

        if ($(this).prop('checked')) {
            $('.check[name="' + name + '"]').prop('required', true)
            text.prop('required', true)
        } else {
            $('.check[name="' + name + '"]').prop('required', false)
            text.prop('required', false)
        }
        
        $('.check[name="' + name + '"]').not(this).prop('checked', false)
        $('.text').not(text).prop('required', 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>

    <div>
        <h5>A. Passport Number</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>B. Voter ID Card</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>C. Driving Licence</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5>D. NREGA Job Card</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5 class="d-inline">E. National Population Register Letter</h5>
        <input type="text" class="text"><br>
        <input type="checkbox" id="OVD" name="OVD" class="check" required>
    </div>

    <div>
        <h5 class="d-inline">F. Proof of Possession on Aadhaar</h5>
        <input type="text">
    </div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文