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

169
Visualizações
Razor show / hide base on radio button using javascript

I am trying to hide or show a textbox, in a ASP MVC razor view, now i using javascript to handle when i click the radio button which can help me to do hide or show function. however, i want to base on database record hide and show the textbox, so if i use below code , i need to click the radio for hide or show a textbox, anyone can give me advise how to hide or show textbox base on database record and no need to click radio button? thanks

<script type="text/javascript">
        const { checked } = require("modernizr");

         function text(x) {

           if  (x == 0 ) document.getElementById("name").style.display = "block",
                document.getElementById("address").style.display = "none",
               
            else  document.getElementById("name").style.display = "none",
                document.getElementById("address").style.display = "block",
            return;

        }

    </script>
   //radio name
   @Html.LabelFor(Model => Model.Type, "A")
   @Html.RadioButtonFor(Model => Model.Type, "A" , new { @onclick = "text(0)", @id = "but1" })
   
   @Html.LabelFor(Model => Model.Type, "B")
@Html.RadioButtonFor(Model => Model.Type, "B" , new { @onclick = "text(1)", @id = "but2" })

//textbox(name)
 <div class="form-group col-md-6" id="name">
  @Html.LabelFor(model => model.name, new { @class = "control-label" })
 @Html.TextBoxFor(model => model.name, new { @class = "form-control"e })
 </div>
 //textbox(address)
  <div class="form-group col-md-6" id="address">
  @Html.LabelFor(model => model.address, new { @class = "control-label" })
  @Html.TextBoxFor(model => model.address, new { @class = "form-control" })
                </div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

<script>
  // self executing function here
(function() {
  // your page initialization code here
  // the DOM will be available here
  text(@Model.Value);
})();
</script>

This is a document ready function. Your text() method will be called when page is loaded. Use @Model.Value your initial db value. That way it will work for the first time with the value which is in the db.

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