Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
How to checkbox enable/disable with Struts 2

I would like to show 2 checkboxs in my page (called box1 and box2). The default value is box1 = false and box2 = false. But I also have to enable box2 only if box1 = true. So I have created the following JS function :

function myTest() {
  var check = document.getElementById('box1');
  if (check.checked == false) {
    $('#box2').prop('disabled', true);
  } else {
    $('#box2').prop('disabled', false);
  }
}

Now I don't really understand how to use myTest function in the JSP file. I have tried to use it with onload="myTest()" as follow, but it doesn't work (box2 is always disabled).

<body onload="myTest()" />

<div class="form-group row">
    <label class="col-sm-3 col-form-label required">
        <s:text name="box1" />
    </label>
    <div class="col-sm-9">
        <s:checkbox id="box1" name="box1"> </s:checkbox>
    </div>
</div>
<div class="form-group row">
    <label class="col-sm-3 col-form-label required">
        <s:text name="box2" />
    </label>
    <div class="col-sm-9">
        <s:checkbox id="box2" name="box2"></s:checkbox>
    </div>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is onchange attribute for <s:checkbox> tag which is used for

Set the html onchange attribute on rendered html element

This tag generates a simple input field of checkbox type.

Renders an HTML input element of type checkbox, populated by the specified property from the ValueStack.

The action bean is on top of the ValueStack, so you can get the specified property there.

To define JS function in JSP you should use html <script> tag. You can do it in the <head> tag or after the rendered element.

<s:checkbox id="box1" name="box1" onchange="myTest()"/>
<s:checkbox id="box2" name="box2" %{box1?'':'disabled="disabled"'}/>

<script>
  function myTest() {
    if ($(this).prop("checked") === "checked") {
      $("#box2").removeAttr("disabled");
    } else {
      $("#box2").attr("disabled","disabled");
    }
  }
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

It seems you are using jQuery in the page. Here is my solution.

<script type="text/javascript">
$( document ).ready(function() {
    // on page load unchecking and disabling box2
    $("#box2").prop("checked", false);
    $("#box2").prop("disabled", true);
});



$("#box1").on("change", function(){
    if ($(this).prop("checked")){
        // box1 is checked
        $("#box2").prop("disabled", false);
    }
    else{
        // box1 is not checked
        $("#box2").prop("checked", false);
        $("#box2").prop("disabled", true);
    }
});
 </script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda