Home >Web Front-end >JS Tutorial >How to Disable and Enable Submit Buttons with jQuery Based on Text Field Value?
In order to control the enabled/disabled state of a submit button based on the value of a text field, the following jQuery code can be implemented:
$(document).ready(function() {</p> <pre class="brush:php;toolbar:false"> $(':input[type="submit"]').prop('disabled', true); $('input[type="text"]').keyup(function() { if($(this).val() != '') { $(':input[type="submit"]').prop('disabled', false); } else { $(':input[type="submit"]').prop('disabled', true); } });
});
This code accomplishes the following:
As a result, the submit button remains disabled until the text field contains a value. Once a value is entered, the submit button becomes enabled, allowing the form to be submitted. If the value in the text field is subsequently deleted, the submit button will be disabled again, preventing the form submission.
The above is the detailed content of How to Disable and Enable Submit Buttons with jQuery Based on Text Field Value?. For more information, please follow other related articles on the PHP Chinese website!