简体   繁体   English

我有多个具有顺序ID的选择,如何在jquery中引用其中之一?

[英]I have multiple select with sequential ids, how can I reference one of them in jquery?

I have a form where the user presses a button and dynamically generates a row of a table, which has 3 columns. 我有一个用户按下按钮并动态生成表的行的表格,该表有3列。 Each generated row is added to the table and has the following form: 每个生成的行将添加到表中,并具有以下格式:

<tr id="1" class="">
  <td>
    <select id="cmb_articulo_1" name="articulo[]">
  </td>
  <td>
    <input type="text" id="txtcantidad_1" name="txtcantidad[]">
  </td>
  <td>
    <input type="text" id="txtprecio_1" name="txtprecio[]">
  </td>
  <td>
    <select id="cmb_descuento_1" name="descuento[]">
  </td>
</tr>

Each element of the generated row is added a sequential corresponding to the generated row ( id="cmb_articulo_1", id="cmb_articulo_2", id="cmb_articulo_3",..) 向生成的行的每个元素添加一个与生成的行相对应的顺序( id="cmb_articulo_1", id="cmb_articulo_2", id="cmb_articulo_3",..)

The user may have generated n-rows and the problem is that I have not been able to determine how to identify in which select from which row an option was chosen and how to process that option. 用户可能已生成n行,但问题是我无法确定如何识别从哪个行中选择了哪个选项,以及如何处理该选项。

How to process the chosen option, has to do with when, for example, I have a select with id = "combo" , I can process the chosen option with $("#combo").change(function{ . . . }) , But in the situation I raise, there may be 1, 2, 5, 10, 20, etc. rows created dynamically, so I can not process the chosen option as in the previous case. 如何处理选定的选项,例如,当我有一个id = "combo"的select时,可以使用$("#combo").change(function{ . . . })处理选定的选项$("#combo").change(function{ . . . }) ,但在我提出的情况下,可能会动态创建1、2、5、10、20等行,因此我无法像上一种情况那样处理所选的选项。 I understand that there must be a way to process the chosen option in a select of any row, making a dynamic reference of that element. 我知道必须有一种方法可以处理任意行中选择的选项,从而动态引用该元素。

Please, can you help me with these two consultations ?, from now, thank you very much. 拜托,您能帮我进行这两次咨询吗?从现在开始,非常感谢。

Add a class to all <select> elements in the table: 将一个类添加到表中的所有<select>元素:

<select class="cmb_articulo" id="cmb_articulo_1" name="articulo[]">

Then use event delegation to be able to bind dynamically inserted elements: 然后使用事件委托绑定动态插入的元素:

$(document).on('change', '.cmb_articulo', function () {
  var $select = $(this);
  var $row = $select.closest('tr');
  var id = $row.attr('id');

  // You have the ID now, do whatever you need to.
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择在Jquery中由循环动态生成的多个ID并将其传递给AJAX? - How can I select multiple ids generated by a loop dynamically in Jquery and pass them to AJAX? 当我在 Javscript 中有多个 arrays 时,如何将它们一一组合? - How can I combine them one by one when I have multiple arrays in Javscript? 如何在不创建单独的 ID 的情况下在多个项目上使用一个函数? - how can i have one function be used on multiple items without creating seperate ids? 如何在一页上具有多种jQuery按钮样式? - How can I have multiple jQuery button styles on one page? jQuery:如何从一条路径中选择多个元素? - jQuery: How can I select multiple elements from one path? 如何在Google API中拥有多个客户端ID? - How can I have multiple Client IDs in Google API? jQuery Selector:当我有多个时,如何指定一个选择菜单? - jQuery Selector: how can I specify one select menu when I have more than one? 如何仅用一个类覆盖多个 ID 的样式? - How can I override the styling of multiple IDs with just one class? jQuery Mobile我有两个页面,一个是android,另一个是iphone。 如何检测浏览器并分配浏览器? - Jquery Mobile I have two page one for android and one for iphone. How can i detect the browser and assign them? 如何有一个包含多个多个div的div可能是4-6-8我如何使用Jquery使它们适合页面加载 - How can have a div which contain multiple multiple div it may 4-6-8 how can i fit them on page load using Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM