I'm attempting to monitor changes to an existing checkbox in a form, and echo a specific message based on whether the checkbox is checked or not.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>test</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<form id="form_for_debugging" action='#' method='POST'>
<p>
<input type="text" name='input_number' value="111" maxlength="6" placeholder="#">
<p>
<input type="checkbox" name='cb_field' id='cb_field' value='1' > Check Me
</form>
<script>
$('#cb_field').change(function()
{
alert('change in cb_field detected...');
if ($('cb_field').prop('checked'))
{
alert('cb_field IS checked!');
} else {
alert('cb_field is NOT checked!');
}
});
</script>
</body>
</html>
The issue is that the if statement returns false (cb_field is NOT checked
) whether the checkbox is actually checked or not.
Am I using the wrong property to test for this or am I missing something else important?
Aucun commentaire:
Enregistrer un commentaire