jeudi 22 septembre 2016

Disable inputs if checkbox is checked

I have a ticket purchase form. You have to fill all the peronal information there. It is also possible to buy a empty ticket, without any name on it. It just requires clicking on the checkbox, which makes all the input fields disabled. var inputsDisabled = 0;

    $("#three").change(function(){

       if(inputsDisabled == 0){

           $("input[name=fname]").attr("disabled", true);
           $("input[name=lname]").attr("disabled", true);
           $("input[name=email]").attr("disabled", true);
           $("input[name=sponsor]").attr("disabled", true);
           $("input[name=phone]").attr("disabled", true);

           inputsDisabled = 1;
       }
       else{

           $("input[name=fname]").attr("disabled", false);
           $("input[name=lname]").attr("disabled", false);
           $("input[name=email]").attr("disabled", false);
           $("input[name=sponsor]").attr("disabled", false);
           $("input[name=phone]").attr("disabled", false);

           inputsDisabled = 0;
       }
    });

When someone buys an empty ticket and presses a "back" browser button, he gets back to this form. The mentioned checkbox is still checked automatically but the fields are not disabled anymore. I tried to use the code below but it doesnt help.

if ("#three".checked) {
            $("input[name=fname]").attr("disabled", true);
            $("input[name=lname]").attr("disabled", true);
            $("input[name=email]").attr("disabled", true);
            $("input[name=sponsor]").attr("disabled", true);
            $("input[name=phone]").attr("disabled", true);
        }

Is there any better way to do this?




Aucun commentaire:

Enregistrer un commentaire