[英]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.