Mark
Mark

Reputation: 4873

Jquery - Conditional not working

I have a function that has multiple conditions, but for some reason, it is getting stuck on the first if statement. Does anyone see why this would be happening?

The var k always returns correct, but no matter what option I select, the first if statement is the only one that runs, despite the value of k.

The jQuery/JS is as follows:

<script>
    $(function () {
        var a = $("#BreedingSireUnReg"),
            b = $("#BreedingDamUnreg"),
            c = $("#BreedingDateUnreg");
        $("#BreedingOptionsUnReg").change(function () {
            var k = parseInt($("#BreedingOptionsUnReg").val());
            console.log(k);
            if (k == 2||8) {
                $(a, b, c).prop("disabled", true);
                console.log("1");
            }
            else if (k == 3||4||5||9||10||11) {
                $(a, b).prop("disabled", true);
                $(c).prop("disabled", false);
                console.log("2");
            }
            else {
                $(a, b, c).prop("disabled", false);
                console.log("3");
            }
        });
    });
</script>

Upvotes: 2

Views: 77

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Change your if statement it is not right, that's the reason it is always going in if block:

if (k == 2|| k ==8)

Upvotes: 1

Mike Cluck
Mike Cluck

Reputation: 32511

Because 8 will always be true. I believe you meant to type if (k == 2 || k == 8). You always have to specify what you're checking against.

Upvotes: 6

Related Questions