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

161
Views
Cómo hacer que los botones funcionen de forma independiente

Así que tengo esta tabla y he agregado algunos botones dentro de las celdas de la tabla.

 <table border="1"> <tr> <th><button id="0" class="mybtn" >1</button><div class="myDiv"></div></th> <th><button id="1" class="mybtn">2</button><div class="myDiv"></div></th> <th><button id="2" class="mybtn" >3</button><div class="myDiv"></div></th> </tr> </table>

Cuando hago clic en los botones, quiero que agreguen un campo de texto. Pero cuando hago clic en él, los tres botones funcionan al mismo tiempo.

Aquí está mi código jQuery

 $(document).ready(function() { $(".mybtn").on("click",function() { $(".myDiv").append("<div><br><input type='text' /><br></div>"); }); });

¿Cómo hacer que los botones funcionen de forma independiente?

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

0

Utilice parent('th').find('.myDiv') .

 $(".mybtn").on("click", function() { $(this).parent('th').find('.myDiv').append("<div><br><input type='text' /><br></div>"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table border="1"> <tr> <th><button id="0" class="mybtn">1</button> <div class="myDiv"></div> </th> <th><button id="1" class="mybtn">2</button> <div class="myDiv"></div> </th> <th><button id="2" class="mybtn">3</button> <div class="myDiv"></div> </th> </tr> </table>

about 4 years ago · Juan Pablo Isaza Report

0

Todo lo que tienes que hacer es usar $(this).next(".myDiv")

 $(document).ready(function() { $(".mybtn").on("click", function() { $(this).next(".myDiv").append("<div><br><input type='text' /><br></div>"); }); });

Manifestación

 $(document).ready(function() { $(".mybtn").on("click", function() { $(this).next(".myDiv").append("<div><br><input type='text' /><br></div>"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table border="1"> <tr> <th><button id="0" class="mybtn">1</button> <div class="myDiv"></div> </th> <th><button id="1" class="mybtn">2</button> <div class="myDiv"></div> </th> <th><button id="2" class="mybtn">3</button> <div class="myDiv"></div> </th> </tr> </table>

about 4 years ago · Juan Pablo Isaza Report

0

Bueno, tiene 3 divs que tienen la clase "myDiv", por lo que el código se ejecuta en los tres si hace clic en cualquier botón:

 $(".myDiv").append("<div><br><input type='text' /><br></div>");

Trabaje con ID o seleccione el contenedor principal y use jQuery $(this) y find() para buscar el div con la clase "myDiv" SOLAMENTE en el contenedor principal.

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!