英文:
Getting Dynamically added table row selected Id from the Javascript
问题
在我的应用程序中,此处使用循环分配的动态ID添加新行。
在视图中,我正在创建采购订单,这里我将行添加到表中。
所以在这里,当“Select”值更改时,我希望获取已更改的行ID和所选ID,以便通过Javascript将其发送到控制器。
如何在javascript中获取它?
var counter = 1;
$(function () {
$("#add").click(function () {
var newRow = $(
'<tr id="tablerow' +
counter +
'"> ' +
'<td>' +
'<label id="CountItems"><strong>' +
counter +
'</strong></label>' +
'</td>' +
'<td width="40%">' +
'<select class="form-select js-dropdown" name="Item_Id[' +
counter +
']" id="ItemId' + counter + '" required="required" ' +
'</select>' +
'</td>' +
'<td width="10%">' +
'<input type="text" class="form-control" name="Qty[' +
counter +
']" value="1" id="Qty' + counter + '" required="required" />' +
'</td>' +
'<td width="10%">' +
'<input type="text" class="form-control" name="AcidStables[' +
counter +
']" value="" required="required" />' +
'</td>' +
'<td width="20%">' +
'<input type="text" class="form-control" name="Unit_Price[' +
counter +
']" value="0.00" id="UnitPrice' + counter + '" required="required" />' +
'</td>' +
'<td width="20%">' +
'<input type="text" class="form-control" name="Line_Total[' +
counter +
']" value="0.00" id="LineTotal' + counter + '" required="required" />' +
'</td>' +
'<td>' +
'<button type="button" class="btn btn-danger" onclick="removeTr(' +
counter +
');">x</button>' +
'</td>' +
'</tr>'
);
var selectElement = newRow.find("select"); // 在新行中查找<select>元素
// 使用选项填充<select>元素
dropdownOptions.forEach(function (option) {
var optionElement = $("<option>").val(option.Value).text(option.Text);
selectElement.append(optionElement);
});
newRow.appendTo("#submissionTable");
counter++;
return false;
});
});
英文:
In my application, Here adding new rows with the dynamic ID assigned from the loop.
In the view, I'm creating a Purchase Order, and here I adding the rows to the table.
So Here When the Select
value changes, I want to get that changed row Id and the Selected Id for a Javascript to send it to the controller.
How to get that in javascript?
var counter = 1;
$(function () {
$("#add").click(function () {
var newRow = $(
'<tr id="tablerow' +
counter +
'"> ' +
"<td>" +
'<label id="CountItems"><strong>' +
counter +
"</strong></label>" +
"</td>" +
'<td width="40%">' +
'<select class="form-select js-dropdown" name="Item_Id[' +
counter +
']" id="ItemId"' + counter+ 'required="required" ' +
"</select>" +
"</td>" +
'<td width="10%">' +
'<input type="text" class="form-control" name="Qty[' +
counter +
']" value="1" id="Qty"' + counter + ' required="required" />' +
"</td>" +
'<td width="10%">' +
'<input type="text" class="form-control" name="AcidStables[' +
counter +
']" value="" required="required" />' +
"</td>" +
'<td width="20%">' +
'<input type="text" class="form-control" name="Unit_Price[' +
counter +
']" value="0.00" id="UnitPrice"' + counter + ' required="required" />' +
"</td>" +
'<td width="20%">' +
'<input type="text" class="form-control" name="Line_Total[' +
counter +
']" value="0.00" id="LineTotal"' + counter + ' required="required" />' +
"</td>" +
"<td>" +
'<button type="button" class="btn btn-danger" onclick="removeTr(' +
counter +
');">x</button>' +
"</td>" +
"</tr>"
);
var selectElement = newRow.find("select"); // Find the <select> element in the new row
// Populate the <select> element with options
dropdownOptions.forEach(function (option) {
var optionElement = $("<option>").val(option.Value).text(option.Text);
selectElement.append(optionElement);
});
newRow.appendTo("#submissionTable");
counter++;
return false;
});
答案1
得分: 0
我在一些地方修改了你的代码:
-
将行内容放入变量以提高可读性。
-
修正了每行的
<select>
填充方式。
它正在起作用。
// 用于测试的下拉列表的假值
var dropdownOptions = [{
Text: "a",
Value: "1"
},
{
Text: "b",
Value: "2"
},
{
Text: "c",
Value: "3"
}
];
var counter = 1;
$(function() {
$("#add").click(function() {
// 创建一行,将内容添加到其中并追加到表格
$('<tr id="tableRow' + counter + '">' + rowContent + '</tr>').appendTo("#submissionTable");
counter++;
return false;
});
});
// 每行的内容
var rowContent = "<td>" +
'<label id="CountItems"><strong>' +
counter +
'</strong></label>' +
"</td>" +
'<td width="40%">' +
'<select onchange="sendInfo(this)" class="form-select js-dropdown" name="Item_Id[' +
counter +
']" id="ItemId' + counter + 'required="required">' + populate() +
'</select>' +
'</td>' +
'<td width="10%">' +
'<input type="text" class="form-control" name="Qty[' +
counter +
']" value="1" id="Qty' + counter + 'required="required" />' +
'</td>' +
'<td width="10%">' +
'<input type="text" class="form-control" name="AcidStables[' +
counter +
']" value="" required="required" />' +
'</td>' +
'<td width="20%">' +
'<input type="text" class="form-control" name="Unit_Price[' +
counter +
']" value="0.00" id="UnitPrice' + counter + 'required="required" />' +
'</td>' +
'<td width="20%">' +
'<input type="text" class="form-control" name="Line_Total[' +
counter +
']" value="0.00" id="LineTotal' + counter + 'required="required" />' +
'</td>' +
'<td>' +
'<button type="button" class="btn btn-danger" onclick="removeTr(' +
counter +
');">x</button>' +
'</td>';
// 使用选项填充<select>元素
// 此函数在上述rowContent变量中调用
function populate() {
var options = "";
dropdownOptions.forEach(function(option) {
options += $("<option>").val(option.Value).text(option.Text).get(0).outerHTML;
});
return options;
}
// 此函数用于<select>的onchange事件,在上述rowContent变量中设置
function sendInfo(e) {
console.log(e.name, e.value);
console.log(e.closest("tr").id);
// 待办事项:将数据发送到需要的位置
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="add">Add</button>
<table id="submissionTable">
</table>
更新
抱歉,看来我忘了在原始回答中提及行的ID。
好的,要获取行的ID,你可以使用element.closest()函数。
我更新了我的代码片段如下:
1- 为每一行赋予了一个ID(就像你在问题中所做的那样):
$('<tr id="tablerow' + counter + '">' + rowContent + '</tr>').appendTo("#submissionTable");
2- 使用closest()
在sendInfo()
函数中检索了行的ID:
console.log(e.closest("tr").id);
英文:
I modified your code in some places:
1- Put row content in a variable to improve readability
2- Corrected the way each row's <select>
is being populated.
And it's working.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// fake values for dropdown only for testing
var dropdownOptions = [{
Text: "a",
Value: "1"
},
{
Text: "b",
Value: "2"
},
{
Text: "c",
Value: "3"
}
];
var counter = 1;
$(function() {
$("#add").click(function() {
// create a row, add content to it and append to table
$('<tr id="tableRow' + counter +'">' + rowContent + "</tr>").appendTo("#submissionTable");
counter++;
return false;
});
});
// content of each row
var rowContent = "<td>" +
'<label id="CountItems"><strong>' +
counter +
"</strong></label>" +
"</td>" +
'<td width="40%">' +
'<select onchange="sendInfo(this)" class="form-select js-dropdown" name="Item_Id[' +
counter +
']" id="ItemId"' + counter + 'required="required"> ' + populate() +
"</select>" +
"</td>" +
'<td width="10%">' +
'<input type="text" class="form-control" name="Qty[' +
counter +
']" value="1" id="Qty"' + counter + ' required="required" />' +
"</td>" +
'<td width="10%">' +
'<input type="text" class="form-control" name="AcidStables[' +
counter +
']" value="" required="required" />' +
"</td>" +
'<td width="20%">' +
'<input type="text" class="form-control" name="Unit_Price[' +
counter +
']" value="0.00" id="UnitPrice"' + counter + ' required="required" />' +
"</td>" +
'<td width="20%">' +
'<input type="text" class="form-control" name="Line_Total[' +
counter +
']" value="0.00" id="LineTotal"' + counter + ' required="required" />' +
"</td>" +
"<td>" +
'<button type="button" class="btn btn-danger" onclick="removeTr(' +
counter +
');">x</button>' +
"</td>";
//Populate the <select> element with options
// this function is called inside the rowContent variable above
function populate() {
var options = "";
dropdownOptions.forEach(function(option) {
options += $("<option>").val(option.Value).text(option.Text).get(0).outerHTML;
});
return options;
}
// this function is set for onchange event of <select> inside the rowContent variable above
function sendInfo(e) {
console.log(e.name, e.value);
console.log(e.closest("tr").id);
// todo: send data to wherever is needed
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="add">Add</button>
<table id="submissionTable">
</table>
<!-- end snippet -->
update
Sorry, it seems I forgot about row's id in the original answer.
Well, to get row.id you can use element.closest() function.
I updated my code snippet as below :
1- gave each row an id (like you did in the question):
$('<tr id="tablerow' + counter +'">' + rowContent + "</tr>").appendTo("#submissionTable");
2- retrieved row.id in the sendInfo()
function using closest()
:
console.log(e.closest("tr").id);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论