2

i have one chekbox and one textfield

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">

<input class="form-control" type="text" id="usr_zipcode" maxlength="10" name="usr_zipcode" required="required">

how to make jquery, when vehiche1 is check but usr_zipcode is empty or no value, it disable another checkbox , name checkme :

<input type="checkbox" id="checkme">

i try to use below code but not working

$('#vehicle1').change(function () {

    var input1 = $('#usr_zipcode');

    input1.change( function() {
       var empty = ( input1.val() == '' );
       $("#checkme").prop('checked', false); 
       $("#checkme").attr('disabled','disabled');
    });
}).change();

2 Answers 2

1

create a separate function to check or disable another checkbox like below

function checkOrDisableAnotherCheckBox() {
  if($('#vehicle1').is(':checked') && $('#usr_zipcode').val() == ''){
    $("#checkme").attr('disabled', true);
  } else {
    $("#checkme").removeAttr('disabled');
  }
}

$('#vehicle1').change(function () {
  checkOrDisableAnotherCheckBox();
});


$('#usr_zipcode').keyup(function () {
  checkOrDisableAnotherCheckBox();


});
Sign up to request clarification or add additional context in comments.

Comments

1

You can try my code:

$('#vehicle1').change(function () {
   checkEmpty();    
});

function checkEmpty(){ 

  var empty = ( $('#vehicle1').prop('checked') && $('#usr_zipcode').val() == '' );
  if (empty) {
    $("#checkme").prop('checked', false); 
    $("#checkme").attr('disabled','disabled');
  }else {
    $("#checkme").removeAttr("disabled");
  }
}

This is a demo: https://codepen.io/phuongnm153/pen/zQavrN

1 Comment

.change on a input type="text" field is not a correct idea. Look for other appropriate function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.