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

477
Views
La llamada Ajax funciona solo para la primera fila de la tabla y no funciona para las siguientes filas

En mi opinión, estoy mostrando una tabla y en la tabla escribí fuertemente la lista desplegable y, a medida que cambia el elemento seleccionado, llama a la función getPrice (int product_id) a través de una llamada ajax y devuelve el precio del elemento seleccionado, pero solo funciona para la primera fila.

HTML

 <tr class="tr_clone" id="1"> <td> @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"} </td> <td class="product_price" id="product_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product_price, "", "") </td></tr> <tr class="tr_clone1" id="2"> <td> @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"}) </td> <td class="product_price" id="product_price1" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product_price, "", "")</td></tr>

llamada ajax

 $(function () { $('#product_id').change(function () { $.ajax({ type: "POST", url: "/Home/GetPrice", datatype: "Json", data: { product_id: $('#product_id').val() }, success: function (data) { document.getElementById('product_price').innerHTML = data; multiply(); }, error: function (data = 0) { document.getElementById('product_price').innerText = 0; multiply(); } }); }); });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

html

 @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel", @onchange = "gg(this)" })

llamada ajax

 <script> function gg(x) { $.ajax({ type: "POST", url: "/Home/GetPrice // GetPrice is name of actionmathod of controller", datatype: "Json", data: { product_id: $(x).val() }, success: function (data) { //your code here }, error: function () { // your code here } }); } </script>
about 4 years ago · Juan Pablo Isaza Report

0

Debe establecer 2 ID diferentes para cada menú desplegable.

 @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @id = "ddl_product_id_1", @class = "form-control sel"} @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @id = "ddl_product_id_2", @class = "form-control sel"})

Y escriba el evento de cambio del individuo que identifica el menú desplegable.

about 4 years ago · Juan Pablo Isaza Report

0

Dado que está seleccionando 2 productos diferentes, su modelo debe tener 2 propiedades de id: product1_id y product2_id y 2 propiedades de precio: product1_price y product2_price

Así que arregla tu vista en consecuencia

 <tr class="tr_clone" id="1"> <td> @Html.DropDownListFor(model => model.product2_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"} </td> <td class="product_price" id="product_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product1_price, "", "") </td></tr> <tr class="tr_clone1" id="2"> <td> @Html.DropDownListFor(model => model.product2_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"}) </td> <td class="product_price" id="product2_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product2_price, "", "")</td></tr>

y deberías separarte en el cambio también

 $(function () { $('#product1_id').change(function () { $.ajax({ type: "POST", url: "/Home/GetPrice", datatype: "Json", data: { product_id: $('#product1_id').val() }, success: function (data) { document.getElementById('product1_price').innerHTML = data; multiply(); }, error: function (data = 0) { document.getElementById('product1_price').innerText = 0; multiply(); } }); }); $('#product2_id').change(function () { $.ajax({ type: "POST", url: "/Home/GetPrice", datatype: "Json", data: { product_id: $('#product2_id').val() }, success: function (data) { document.getElementById('product2_price').innerHTML = data; multiply(); }, error: function (data = 0) { document.getElementById('product2_price').innerText = 0; multiply(); } }); }); });
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!