Matt Drake
Matt Drake

Reputation: 156

Disable button on if...else

I have some JQuery which I would like to use to disable a button on a php page should a particular result come back.

JQuery

...
success:function(data){
            $("#value10").html(data.price);
            $("#total").html(data.newtotal);
            $("#toohigh").html(data.toohigh);
            if(data.toohigh === 1) {
                $("#subbut").attr('disabled', 'disabled');
            } else if(data.toohigh === 0){
                $("#subbut").attr('disabled', 'disabled');
            }

php

<button type="submit" name="Submit" class="btn btn-primary align-content-center" id="subbut">

I've tried all sorts of different possible solutions none of which disable the button when data.toohigh equals 1

What have I missed?

Upvotes: 1

Views: 406

Answers (2)

Faheem azaz Bhanej
Faheem azaz Bhanej

Reputation: 2396

It's caused by your variable data.toohigh is not matched with your condition 1 or 0 or problem with datatype

Try this code

...
success:function(data){
            $("#value10").html(data.price);
            $("#total").html(data.newtotal);
            $("#toohigh").html(data.toohigh);
            if(data.toohigh == '1') {
                $("#subbut").attr('disabled', 'disabled');
            } else if(data.toohigh == '0'){
                $("#subbut").attr('disabled', 'disabled');
            }

Upvotes: 1

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

you can try using prop like below:

   if(data.toohigh === 1) {
        $("#subbut").prop('disabled', true);
    } else if(data.toohigh === 0){
        $("#subbut").prop('disabled', false);
    }

Upvotes: 0

Related Questions