简体   繁体   English

单击复选框时如何禁用文本框?

[英]How to disabled textfield when i click the checkbox?

I wanted to disable the div when i click the the checkbox. 我想在单击复选框时禁用div。 if the current address and permanent address are the same, the user just click the check box to disabled the field of the permanent address. 如果当前地址和永久地址相同,则用户只需单击复选框即可禁用永久地址的字段。 Thanks! 谢谢!

<div class="col-sm-5">
        <div class="checkbox">
            <label>
                <input type="checkbox">Current and Permanent Address are the same.
            </label>
        </div>
    </div>
     <div rv-each-address="applicant:personal_information:addresses">
        <div class="row">
           <div class="col-md-8">
              <div class="form-group">
                 <label class="control-label"><strong  rv-text="address:description">Permanent Address</strong></label>
                 <input class="form-control" rv-value="address:address"
                    name="model.cid" data-validate="required" />
              </div>
           </div>
           <div class="col-md-4">
              <div class="form-group">
                 <label class="control-label" for="city">City</label> <input
                    rv-value="address:city" rv-enabled="address:province" type="text" class="form-control typeahead" name="city"
                    id="city" data-validate="required"
                    placeholder="Current city" />
              </div>
           </div>
        </div>
        <div class="row">
           <div class="col-md-4">
              <div class="form-group">
                 <label class="control-label" for="state">Province</label> <input
                    rv-value="address:province" rv-enabled="address:country" type="text" id="province" placeholder="Select Province"
                    name="province" class="form-control typeahead">
              </div>
           </div>
           <div class="col-md-4">
              <div class="form-group">
                 <label class="control-label" for="country">Country</label> 
                 <select name="gender" class="form-control" id="gender">
                 <option value="" disabled selected>Country</option>
                 <option>Philippines</option>
                 <option>Hong Kong</option>
                 <option>South Korea</option>
                 <option>Singapore</option>
                 <option>China</option>
              </select> 
              </div>
           </div>

           <div class="col-md-4">
              <div class="form-group">
                 <label class="control-label" for="postalCode">Postal
                 Code</label> <input class="form-control" rv-value="address:postalCode" name="postalCode"
                    id="postalCode" data-validate="required"
                    placeholder="Zip Code" />
              </div>
           </div>
        </div>
     </div>

Add the following JS to your code. 将以下JS添加到您的代码中。

 $(document).ready(function(){ $('input[type="checkbox"]').change(function(){ $('.permanentAdd').attr('disabled','disabled'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-sm-5"> <div class="checkbox"> <label> <input type="checkbox">Current and Permanent Address are the same. </label> </div> </div> <div rv-each-address="applicant:personal_information:addresses"> <div class="row"> <div class="col-md-8"> <div class="form-group "> <label class="control-label"><strong rv-text="address:description">Permanent Address</strong></label> <input class="form-control permanentAdd" rv-value="address:address" name="model.cid" data-validate="required" /> </div> </div> <div class="col-md-4"> <div class="form-group"> <label class="control-label" for="city">City</label> <input rv-value="address:city" rv-enabled="address:province" type="text" class="form-control typeahead" name="city" id="city" data-validate="required" placeholder="Current city" /> </div> </div> </div> <div class="row"> <div class="col-md-4"> <div class="form-group"> <label class="control-label" for="state">Province</label> <input rv-value="address:province" rv-enabled="address:country" type="text" id="province" placeholder="Select Province" name="province" class="form-control typeahead"> </div> </div> <div class="col-md-4"> <div class="form-group"> <label class="control-label" for="country">Country</label> <select name="gender" class="form-control" id="gender"> <option value="" disabled selected>Country</option> <option>Philippines</option> <option>Hong Kong</option> <option>South Korea</option> <option>Singapore</option> <option>China</option> </select> </div> </div> <div class="col-md-4"> <div class="form-group"> <label class="control-label" for="postalCode">Postal Code</label> <input class="form-control" rv-value="address:postalCode" name="postalCode" id="postalCode" data-validate="required" placeholder="Zip Code" /> </div> </div> </div> </div> 

You can try something like this 您可以尝试这样的事情

    <input type="checkbox" id="check_box" onchange="set_status();">
            <div class="col-md-4">
                          <div class="form-group">
                             <label class="control-label" for="postalCode">Postal
                             Code</label> <div id="txt_field" name="txt_field"> </div>   

                <script type="text/javascript">


function set_status() {
        var appendData = '';
        var checkbox = document.getElementById("check_box");
        if (checkbox.checked == true) {
            appendData = '<input class="form-control" rv-value="address:postalCode" name="postalCode" id="postalCode" readonly="readonly" data-validate="required"   placeholder="Zip Code" />';
        }
        else {
            appendData = '<input class="form-control" rv-value="address:postalCode" name="postalCode" id="postalCode" data-validate="required"   placeholder="Zip Code" />';
        }
        window.jQuery('#txt_field').html(appendData);

    }

            </script>
</div>
           </div>
        </div>
     </div>

Here is the example how you do this thing easy. 这是您如何轻松完成此操作的示例。 make sure you have only one checkbox in this window. 确保在此窗口中只有一个复选框。 else this not working, then you need to change something. 否则这不起作用,那么您需要进行一些更改。

jQuery jQuery的

$( "input[type=checkbox]" ).on( "click", function(){
    var n = $("input[type=checkbox]:checked").length;
    if(n){
        $(".permanent-address").attr("disabled", 'disabled');
    }else{
        $(".permanent-address").removeAttr("disabled");
    }
});

HTML 的HTML

<div class="checkbox">
    <label>
        <input type="checkbox">Current and Permanent Address are the same.
    </label>
</div>

<input class="form-control permanent-address" rv-value="address:address" name="model.cid" data-validate="required" />

Pure javascript anwser 纯JavaScript Anwser

function checkAddress(checkbox) {
  var inputs = document.querySelectorAll('input:not([type=checkbox])');
  var selects = document.querySelectorAll('select');
  for (var i = 0; i < inputs.length; i++) {
    inputs[i].disabled = checkbox.checked;
  }
  for (var i = 0; i < selects.length; i++) {
    selects[i].disabled = checkbox.checked;
  }
}

HTML code HTML代码

<input type="checkbox" onclick="checkAddress(this)">

https://jsfiddle.net/0ba4f7jd/ https://jsfiddle.net/0ba4f7jd/

Simply create a function that gets called when the checkbox is checked, which disables the div and all child elements: 只需创建一个在选中复选框时会调用的函数,即可禁用div和所有子元素:

 $('#checkbox').change(function(){ var div = $('#everything'); if (div.attr('class')!="disabled") { div.addClass("disabled"); $("#everything *").attr("disabled", true); } else { div.removeClass("disabled"); $('#everything *').attr('disabled',false); } }); 

DEMO 演示

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

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