Skip to main content
Inspiring
May 13, 2021 6:56 PM UTC
Question

Have form grayed out until check box is checked?

Hello we use Coldfusion 10 and I'm having issues with a form and having it be grayed out until a check box is selected. Here's a small part of the field I'm trying to do. What an I missing?

 

<form action="TransactD.cfm" Target="body" method="post">
<input type="hidden" name="Receiveall" value="1">
<input type="submit" value="Receive all" disabled class="rabutton" onClick="this.form.submit();">&nbsp;<input type="checkbox" name="checkbox-0" class="rabutton">
</form>


<script id="jsbin-javascript">
$(".rabutton").on("change", function(e){
if($(".rabutton").attr("checked")){
$(".submit").button("enable");
} else {
$(".submit").button("disable");
}

});
</script>

 

Any help would be great thanks.

    This topic is closed to new replies. Start a new post to keep the conversation going.

    8 replies

    James Moberg
    Inspiring
    May 20, 2021 2:51 PM UTC

    If you want to block a "form" (ie, more than just a single output or a button), I recommend using a jQuery plugin like BlockUI. http://malsup.com/jquery/block/  (Another alternative solution would be to "hide" the section.)

     

    We've used this plugin to display-but-block access to sections of a page until conditions are met.  We've also used this plugin to prevent interaction while background actions are performed and/or the content is being refreshed.

    (NOTE: Please be aware that blocking/disabling is usually purely cosmetic and can be bypassed by accessing the browser's console and adding inline overrides or disabling event handlers.)

    Translate
    BKBK
    Community Expert
    Community Expert
    May 14, 2021 12:00 PM UTC

    The Javascript is a bit confusing. That is because the submit button and the checkbox both belong to the same class, rabutton.

     

    Suggestion for a solution: identify the submit button using the selector "input:submit", then simplify. My test code is as follows

     

     

    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    
    <script id="jsbin-javascript">
    $(document).ready(function() {
    	$(".rabutton").change(function(){
    		if(this.checked){
    			$("input:submit").attr("disabled", false);
    		} else {
    			$("input:submit").attr("disabled", true);
    		}
    	});
    });
    </script>
    
    
    <form action="TransactD.cfm" Target="body" method="post">
    <input type="hidden" name="Receiveall" value="1">
    <input type="submit" value="Receive all" disabled class="rabutton" onClick="this.form.submit();">&nbsp;<input type="checkbox" name="checkbox-0" class="rabutton">
    </form>
    <head>
    

     

     

    Translate
    BKBK
    Community Expert
    Community Expert
    May 19, 2021 7:18 PM UTC

    Hi @taunnt ,

    Is it working now?

    Translate
    Participating Frequently
    May 13, 2021 7:49 PM UTC

    I would refer to the objects by an ID rather than CLASS so you can target specific objects and when you are reading and setting properties of objects use the JQuery .prop() function.

     

    I assume you are loading the JQuery library in your page also.

     

    There are probably many ways to do this but here is my version of the revised code.

    <body>
    <form action="TransactD.cfm" Target="body" method="post">
    <input type="hidden" name="Receiveall" value="1">
    <input type="submit" value="Receive all" disabled id="rabutton" onClick="this.form.submit();">&nbsp;<input type="checkbox" name="checkbox-0" id="racb">
    </form>
    </body>
    <script type="text/javascript">
    $("#racb").on("change", function(e){
    if($("#racb").prop("checked")){
    $("#rabutton").prop('disabled', false);
    } else {
    $("#rabutton").prop('disabled', true);
    }
    });
    </script>

    Translate