I have a strange problem here with (un)checked radio buttons.
My code:
function validate(el) {
if ($('#type_P').is(':checked') === true && (parseInt($('#voucher_price').val()) <= 0) || isNaN(parseInt($('#voucher_price').val()))) {
alert($('#type_P').is(':checked') === true);
$('#service_price').addClass('error');
$('#service_price').parent().addClass('error');
}
return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form onsubmit="return validate(this)">
P: <input type="radio" checked name="type" id="type_P" value="P">
S: <input type="radio" name="type" id="type_S" value="S">
<input id="voucher_price">
<input type=submit>
</form>
And it returns me 'false' in alert when 'type_S' is checked, not 'type_P'. I mean no alert has to be shown.
Why? How is possible to show this alert when the same condition is in the if statement?
As comments under question said, there was a problem in brackets. Right is (expr1 && (expr2 || expr3))
, I had (expr1 && expr2 || expr3)
. It was just a typo from me. Thanks for both commentators.
Nothing special, I just need to answer this question to move it from the 'unanswered' list to avoid another people lose their time on it.