I can not enable the disabled state in a form: checkbox

1

I'm working with Spring webflow and I have a form: switchbox checkbox element in my jsp that by default must be disabled. I'm going crazy trying to enable it using JQuery when a condition is met.

<form:checkbox id="anId" path="" value="" cssClass="js-switch" disabled="true"/>

The html code it generates is like this:

<input id="anId" class="js-switch" disabled="" type="checkbox" value="" data-switchery="true" style="display: none;">
<span class="switchery" id="anId" style="">
<small style=""></small>
</span>

I have tried changing the disabled to true, removing the disabled attribute, too:

$("#anId").trigger("chosen:updated");

How should I implement this to make it work?

I frame your correct answer because with the information I gave, it is. The problem lies in:

<span style="patatan:a_lot_of_stuff;"  class="switchery" id="anId"  />

I did not copy for brevity, it is who gives the appearance of disabled to it. Knowing that, it's just:

$("span#anId").attr("style", "pataton:otra_castaña;");

and

<small style="etc" />

well more of the same:

$("span#anId small").attr("style", "stuff");

With this the click event does not work or forcefully, I tried:

$("span#anId").off("click");

... at the beginning (I do not know why) but then I used it when I want it to be disabled again and behave as such. What I did:

$("div#el_que_lo_contiene").click(function(){
    if($("span#anId small").css("left")=="20px"){
        $("span#anId small").attr("style", "left:0px;y:mas;"); 
        $("input#anId").val(false);
    } else {
        $("span#anId small").attr("style", "left:20px;y:mas;"); 
        $("input#anId").val(true);
    }
});

To send the value that contains the input (which is who actually stores it) I do something like this:

$("send").click(function(){
    $("input#anId").removeAttr("disabled");
}); 

and should reach the controller ... Then control the behavior and ready.

    
asked by Luis 25.01.2017 в 13:22
source

1 answer

1

When you click on a button that you want to disable or activate a checkbox, you should use the following code:

link

$("button").on("click", function() {
    if($("input[type='checkbox']").prop("disabled")) {
        $("input[type='checkbox']").prop("disabled", false)
    }
    else {
        $("input[type='checkbox']").prop("disabled", true)
    }
});

Here what it does is check if the property disabled is active or not, and depending on it disables the checkbox or not.
Here you will have to put id's, classes, or whatever you want to do.

    
answered by 25.01.2017 / 14:10
source