从JavaScript中获取动态添加的表格行的选定ID。

huangapple go评论76阅读模式
英文:

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 () {
$(&quot;#add&quot;).click(function () {
var newRow = $(
&#39;&lt;tr id=&quot;tablerow&#39; +
counter +
&#39;&quot;&gt; &#39; +
&quot;&lt;td&gt;&quot; +
&#39;&lt;label id=&quot;CountItems&quot;&gt;&lt;strong&gt;&#39; +
counter +
&quot;&lt;/strong&gt;&lt;/label&gt;&quot; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;40%&quot;&gt;&#39; +
&#39;&lt;select class=&quot;form-select js-dropdown&quot; name=&quot;Item_Id[&#39; +
counter +
&#39;]&quot; id=&quot;ItemId&quot;&#39; + counter+ &#39;required=&quot;required&quot; &#39; +
&quot;&lt;/select&gt;&quot; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;10%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Qty[&#39; +
counter +
&#39;]&quot; value=&quot;1&quot; id=&quot;Qty&quot;&#39; + counter + &#39; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;10%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;AcidStables[&#39; +
counter +
&#39;]&quot; value=&quot;&quot; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;20%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Unit_Price[&#39; +
counter +
&#39;]&quot; value=&quot;0.00&quot; id=&quot;UnitPrice&quot;&#39; + counter + &#39;  required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;20%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Line_Total[&#39; +
counter +
&#39;]&quot; value=&quot;0.00&quot; id=&quot;LineTotal&quot;&#39; + counter + &#39; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&quot;&lt;td&gt;&quot; +
&#39;&lt;button type=&quot;button&quot; class=&quot;btn btn-danger&quot; onclick=&quot;removeTr(&#39; +
counter +
&#39;);&quot;&gt;x&lt;/button&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&quot;&lt;/tr&gt;&quot;
);
var selectElement = newRow.find(&quot;select&quot;); // Find the &lt;select&gt; element in the new row
// Populate the &lt;select&gt; element with options
dropdownOptions.forEach(function (option) {
var optionElement = $(&quot;&lt;option&gt;&quot;).val(option.Value).text(option.Text);
selectElement.append(optionElement);
});
newRow.appendTo(&quot;#submissionTable&quot;);
counter++;
return false;
});

答案1

得分: 0

我在一些地方修改了你的代码:

  1. 将行内容放入变量以提高可读性。

  2. 修正了每行的<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 &lt;select&gt; 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: &quot;a&quot;,
Value: &quot;1&quot;
},
{
Text: &quot;b&quot;,
Value: &quot;2&quot;
},
{
Text: &quot;c&quot;,
Value: &quot;3&quot;
}
];
var counter = 1;
$(function() {
$(&quot;#add&quot;).click(function() {
// create a row, add content to it and append to table
$(&#39;&lt;tr id=&quot;tableRow&#39; + counter +&#39;&quot;&gt;&#39; + rowContent + &quot;&lt;/tr&gt;&quot;).appendTo(&quot;#submissionTable&quot;);
counter++;
return false;
});
});
// content of each row
var rowContent = &quot;&lt;td&gt;&quot; +
&#39;&lt;label id=&quot;CountItems&quot;&gt;&lt;strong&gt;&#39; +
counter +
&quot;&lt;/strong&gt;&lt;/label&gt;&quot; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;40%&quot;&gt;&#39; +
&#39;&lt;select onchange=&quot;sendInfo(this)&quot; class=&quot;form-select js-dropdown&quot; name=&quot;Item_Id[&#39; +
counter +
&#39;]&quot; id=&quot;ItemId&quot;&#39; + counter + &#39;required=&quot;required&quot;&gt; &#39; + populate() +
&quot;&lt;/select&gt;&quot; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;10%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Qty[&#39; +
counter +
&#39;]&quot; value=&quot;1&quot; id=&quot;Qty&quot;&#39; + counter + &#39; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;10%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;AcidStables[&#39; +
counter +
&#39;]&quot; value=&quot;&quot; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;20%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Unit_Price[&#39; +
counter +
&#39;]&quot; value=&quot;0.00&quot; id=&quot;UnitPrice&quot;&#39; + counter + &#39;  required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&#39;&lt;td width=&quot;20%&quot;&gt;&#39; +
&#39;&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;Line_Total[&#39; +
counter +
&#39;]&quot; value=&quot;0.00&quot; id=&quot;LineTotal&quot;&#39; + counter + &#39; required=&quot;required&quot; /&gt;&#39; +
&quot;&lt;/td&gt;&quot; +
&quot;&lt;td&gt;&quot; +
&#39;&lt;button type=&quot;button&quot; class=&quot;btn btn-danger&quot; onclick=&quot;removeTr(&#39; +
counter +
&#39;);&quot;&gt;x&lt;/button&gt;&#39; +
&quot;&lt;/td&gt;&quot;;
//Populate the &lt;select&gt; element with options
// this function is called inside the rowContent variable above 
function populate() {
var options = &quot;&quot;;
dropdownOptions.forEach(function(option) {
options += $(&quot;&lt;option&gt;&quot;).val(option.Value).text(option.Text).get(0).outerHTML;
});
return options;
}
// this function is set for onchange event of &lt;select&gt; inside the rowContent variable above
function sendInfo(e) {
console.log(e.name, e.value);
console.log(e.closest(&quot;tr&quot;).id);
// todo: send data to wherever is needed
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;button type=&quot;button&quot; id=&quot;add&quot;&gt;Add&lt;/button&gt;
&lt;table id=&quot;submissionTable&quot;&gt;
&lt;/table&gt;

<!-- 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):

$(&#39;&lt;tr id=&quot;tablerow&#39; + counter +&#39;&quot;&gt;&#39; + rowContent + &quot;&lt;/tr&gt;&quot;).appendTo(&quot;#submissionTable&quot;);

2- retrieved row.id in the sendInfo() function using closest():

console.log(e.closest(&quot;tr&quot;).id);

huangapple
  • 本文由 发表于 2023年5月29日 15:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定