Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda