Disable / Enable button depending on input text

I have a Fiddle , and I ran the script when this box is checked first and the text is inserted into the input, but if I try to insert the text, the first button will not turn it on. Can someone look at this fiddle? this is my script

$('.checkset label').on('change', function () {
var reqlength = $('.important').length;
console.log(reqlength);
var value = $('.important').filter(function () {
    return this.value != '';
});


if ($('.checkset label').hasClass("checked")) {
    if (value.length >= 0 && (value.length !== reqlength)) {
        //alert('Please fill out all required fields.');
        $('#reg').prop('disabled', true);
        $('#reg').addClass('inactive');
    } else {
        //alert('Everything has a value.');
        $('#reg').prop('disabled', false);
        $('#reg').removeClass('inactive');
    }
} else {
    $('#reg').prop('disabled', true);
    $('#reg').addClass('inactive');
}
});
   $(".important").on('change', function () {
   $('.checkset label').change();
});
+4
source share
1 answer

Try to substitute an element inputfor an element labelin changeevents, using .toggleClass()to set the class .checkedon the page .checkset label, if everyone .important inputhas elements, do not assign a value

$(".checkset").click(function(){
  $(this).find("label").toggleClass("checked"); 
});
var res;
var inputs = $(".important, .checkset input");
inputs.on("input change", function() {
  res = inputs.get().every(function(el) {

    return $(el).is("[type=checkbox]") ? el.checked : el.value.length !== 0 
  });
    $("#reg").prop("disabled", !res)
    .toggleClass("inactive", !res)
})

jsfiddle http://jsfiddle.net/r9rsddhk/15/

+1
source

Source: https://habr.com/ru/post/1606148/


All Articles