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

146
Views
cómo mostrar el valor del cuadro oculto cuando se hace clic en el botón en el bucle php

Intento crear un sitio de tienda simple para aprender, pero me quedé atascado al hacer clic en el botón. cuando hago clic no pasa nada. debería mostrar el texto del valor en el botón oculto cuando hago clic en el botón.

a continuación está mi código php

 $i=1; while($rows = mysqli_fetch_assoc($varresultP)){ echo "<button type='button' class='btn btn-outline-primary' onclick='myFunction()' >".$rows['var_name']."</button>"; echo "<input type='hidden' name='price' value=".$rows['price']." id='price{$i}'>"; echo "<input type='hidden' value=".$rows['id']." id='id{$i}'>"; echo "<input type='hidden' value=".$rows['quantity']." id='quantity{$i}'>"; $i++; }

el resultado es que aparecen varios botones, pero no sucede nada cuando hago clic en el botón. El botón total se basa en la base de datos cuántos datos tiene.

a continuación es mi jsquery

 function myFunction() { for(var i=1; i<5; i++){ var x = document.getElementById("price"+i).value; document.getElementById("sendPrice").innerHTML = x; var x = document.getElementById("id"+i).value; document.getElementById("idDiv").innerHTML = x; var x = document.getElementById("quantity"+i).value; document.getElementById("showQuantity").innerHTML = x; } }

valor de id(price) que quiero mostrar en div id(sendPrice) , valor de id(id) que necesito mostrar en el botón href, valor de id(quantity) que necesito aparecerá en div id(showQuantity) cuando el botón está onclick.

a continuación se muestra el código html

 <div class ='row' style='padding: 30px; width: 100px;'><p>USD: <label class='p2--semi-bold c-red' id='sendPrice'></p></label></div> <div class ='row' style='padding: 30px; width: 100px;'><p>Productid: <label id='idDiv'></p></label></div> <div class ='row' style='padding: 30px; width: 100px;'><p>Quantity: <label id='showQuantity'></p></label></div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Primero agregue {$i} a su onclick='myFunction()' como onclick='myFunction({$i})'

segundo en su javascript, elimine el bucle for y agregue i a la function myFunction() { como function myFunction(i) {

Manifestación

 function myFunction(i) { var x = document.getElementById("price" + i).value; document.getElementById("sendPrice").innerHTML = x; var x = document.getElementById("id" + i).value; document.getElementById("idDiv").innerHTML = x; var x = document.getElementById("quantity" + i).value; document.getElementById("showQuantity").innerHTML = x; }
 <button type='button' class='btn btn-outline-primary' onclick='myFunction(1)'>".$rows['var_name']."</button> <input type='hidden' name='price' value=".$rows['price']. ROW1" id='price1'> <input type='hidden' value=".$rows['id']. ROW1" id='id1'> <input type='hidden' value=".$rows['quantity']. ROW1" id='quantity1'>"; <button type='button' class='btn btn-outline-primary' onclick='myFunction(2)'>".$rows['var_name']."</button> <input type='hidden' name='price' value=".$rows['price']. ROW2" id='price2'> <input type='hidden' value=".$rows['id']. ROW2" id='id2'> <input type='hidden' value=".$rows['quantity']. ROW2" id='quantity2'>"; <div id="sendPrice"></div> <div id="idDiv"></div> <div id="showQuantity"></div>

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!