简体   繁体   English

如果文本框不为空,则选中复选框

[英]If text boxes are not empty check checkbox

I am trying to figure out how to check if all 4 inputs are filled out then checkmark the contact information check box. 我试图弄清楚如何检查所有4个输入是否都已填写,然后选中联系信息复选框。 If any are not filled out uncheck the checkbox. 如果未填写,请取消选中该复选框。

Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

 $(document).on('change', '#ContactName, #ContactEmail, #ContactPhone', function() { if ('#ContactName, #ContactEmail, #ContactPhone' === '') { $("#contactinformation").prop("checked", false); } else { $("#contactinformation").prop("checked", true); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactName">Contact name:</label> <input type="text" class="form-control input-sm" name="ContactName" id="ContactName" size="40" maxlength="120" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="BusinessName">Business name:</label> <input type="text" class="form-control input-sm" name="BusinessName" id="BusinessName" size="40" maxlength="120" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactEmail">Email address:</label> <input type="text" class="form-control input-sm" name="ContactEmail" id="ContactEmail" size="40" maxlength="80" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactPhone">Phone number (business hours):</label> <input type="text" class="form-control input-sm" name="ContactPhone" id="ContactPhone" size="40" maxlength="50" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="form-group"> <input type="checkbox" name="contactinformation" id="contactinformation" /> Contact information </div> </div> </div> 

Answer updated to reflect new requirement that BusinessName be optional. 更新了答案,以反映新的要求BusinessName是可选的。

See the comments inline: 参见内联注释:

 // Set up a blur event handler for each text field $('.form-control:not("#BusinessName")').on("blur", function(evt) { let count = 0; // Keep track of how many are filled in // Loop over all the text fields $('.form-control:not("#BusinessName")').each(function(idx, el){ // If the field is not empty.... if(el.value !== ""){ count++; // Increase the count } }); // Test to see if all 3 are filled in if(count === 3){ $("#contactinformation").prop("checked", true); // Check the box } else { $("#contactinformation").prop("checked", false); // Unheck the box } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactName">Contact name:</label> <input type="text" class="form-control input-sm" name="ContactName" id="ContactName" size="40" maxlength="120" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="BusinessName">Business name:</label> <input type="text" class="form-control input-sm" name="BusinessName" id="BusinessName" size="40" maxlength="120" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactEmail">Email address:</label> <input type="text" class="form-control input-sm" name="ContactEmail" id="ContactEmail" size="40" maxlength="80" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-7"> <div class="form-group"> <label for="ContactPhone">Phone number (business hours):</label> <input type="text" class="form-control input-sm" name="ContactPhone" id="ContactPhone" size="40" maxlength="50" value="" /> </div> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="form-group"> <input type="checkbox" name="contactinformation" id="contactinformation" disabled /> Contact information </div> </div> </div> 

Please try with below code snippet. 请尝试使用以下代码段。

<input type="text" class="form-control input-sm InputText" name="BusinessName" id="BusinessName" size="40" maxlength="120" value="" />




 $( document ).ready(function() {
    $(".InputText").change(function(){
var checkCheckBox= true; 
         $(".InputText").each(function() {
                 if ($.trim($(this).val()) != '') {
                     checkCheckBox = false;
                }
                });

         if (checkCheckBox == true)

        {
            $("#contactinformation").prop("checked",true);
        }
        else
        {


    $("#contactinformation").prop("checked",false);
            }
        });
    });

Add "InputText" class in textbox, if you want to validate this thing. 如果要验证此内容,请在文本框中添加“ InputText”类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM