Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Opción de botón de opción predeterminada con jquery

Estoy tratando de ocultar/mostrar diferentes divs usando el método que se muestra aquí .

 <style> .box{ color: #fff; padding: 20px; display: none; margin-top: 20px; } .red{ background: #ff0000; } .green{ background: #228B22; } .blue{ background: #0000ff; } label{ margin-right: 15px; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ $('input[type="radio"]').click(function(){ var inputValue = $(this).attr("value"); var targetBox = $("." + inputValue); $(".box").not(targetBox).hide(); $(targetBox).show(); }); }); </script> </head> <body> <div> <label><input type="radio" name="colorRadio" value="red" checked = "checked"> red</label> <label><input type="radio" name="colorRadio" value="green"> green</label> <label><input type="radio" name="colorRadio" value="blue"> blue</label> </div> <div class="red box">You have selected <strong>red radio button</strong> so i am here</div> <div class="green box">You have selected <strong>green radio button</strong> so i am here</div> <div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div> </body>

Me gustaría que se haga clic en el botón rojo de forma predeterminada cuando se carga la página. Sin embargo, si solo cambio la etiqueta a esto:

 <label><input type="radio" name="colorRadio" value="red" checked = "checked"> red</label>

Se muestra como se hace clic:

verificado pero no muestra

pero tengo que volver a hacer clic en él para cargar realmente el div rojo:

muestra después de hacer clic

¿Hay alguna manera elegante de arreglar eso para que muestre el div rojo cuando se carga la página?

muchas gracias, alex

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Qué tal simplemente agregar $('.red').show() para establecer qué cuadro debe mostrarse como predeterminado?

 $(document).ready(function () { $('input[type="radio"]').click(function () { var inputValue = $(this).attr('value'); var targetBox = $('.' + inputValue); $('.box').not(targetBox).hide(); $(targetBox).show(); }); $('.red').show(); });
 .box { color: #fff; padding: 20px; display: none; margin-top: 20px; } .red { background: #ff0000; } .green { background: #228b22; } .blue { background: #0000ff; } label { margin-right: 15px; }
 <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <body> <div> <label><input type="radio" name="colorRadio" value="red" checked> red</label> <label><input type="radio" name="colorRadio" value="green"> green</label> <label><input type="radio" name="colorRadio" value="blue"> blue</label> </div> <div class="red box">You have selected <strong>red radio button</strong> so i am here</div> <div class="green box">You have selected <strong>green radio button</strong> so i am here</div> <div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div> </body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!