简体   繁体   English

单击Angularjs中的保存按钮时如何从动态创建的输入文本框中获取数据

[英]How to fetch data from dynamically created input textboxes on click of save button in Angularjs

I'm dynamically creating textboxes and save button for each Item and on click of save button I need to fetch value from the text box of that particular item. 我正在为每个项目动态创建文本框和保存按钮,单击保存按钮后,我需要从该特定项目的文本框中获取值。

 for (let d = 0; d <= ItemsInfo.length - 1; d++)
    {                 
           content += '<tr>  <td> <label for="lblPriority">Item Priority </label>  </td>  ';
           content += ' <td>   <input type="text" id="inpItemPRIORITY" ng-model="prty" value=" ' +  ItemsInfo[d].PRIORITY  + ' " /> </td> </tr>';
           content += '<tr>  <td>  <label for="lblItemComment">Item Comment</label></td> ';
           content += ' <td>   <input type="text" id="inpItemCOMMENT"  ng-model="cmnt"  value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>';
            // Save Item
          content += '<tr>  <td>  <button class="get-data" ng-click="buttonClick(prty,cmnt)">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
  }

In controller: 在控制器中:

 $scope.buttonClick = function (prty,cmnt) {
  console.log(prty + " " + cmnt); } // console.log displays as undefined undefined

or is there a better way to do this? 还是有更好的方法来做到这一点?

As a start, the proper AngularJs would be to use ngRepeat directly in your HTML template, but i'm still giving you a solution with your current code: 首先,正确的AngularJs应该是直接在HTML模板中使用ngRepeat ,但是我仍然会为您提供当前代码的解决方案:

First, an HTML id should be unique, you are looping manually on your array in the controller, so you can add an index number to the id, and give it to your function to find the elements (also fixed the for loop condition for optimisation): 首先,HTML ID应该是唯一的,您正在控制器中的数组上手动循环,因此您可以在ID中添加索引号,并将其赋予函数以查找元素(还修复了for循环条件以进行优化) ):

In this case, it's useless to bind to a model, or else all inputs will use the same scope variable. 在这种情况下,绑定到模型是没有用的,否则所有输入将使用相同的作用域变量。 This code also shows the proper way to retrieve a DOM element in jQlite format in AngularJS: 此代码还显示了在AngularJS中以jQlite格式检索DOM元素的正确方法:

for (let d = 0; d < ItemsInfo.length; d++)
{                 
    content += '<tr>  <td> <label for="lblPriority">Item Priority </label>  </td>  ';
    content += ' <td>   <input type="text" id="inpItemPRIORITY_' + d + '" value="' +  ItemsInfo[d].PRIORITY  + '" /> </td> </tr>';
    content += '<tr>  <td>  <label for="lblItemComment">Item Comment</label></td> ';
    content += ' <td>   <input type="text" id="inpItemCOMMENT_' + d + '"  value="' + ItemsInfo[d].COMMENT + '" /> </td> </tr>';
    // Save Item
    content += '<tr>  <td>  <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
}
$scope.buttonClick = function (index) {
    let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ),
        cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) );
    console.log(prty.val() + " " + cmnt.val());
};

Here a snippet that demonstrate. 这是一个演示片段。 just an example to show that it works (EDIT: added $compile directive for buttonClick to work properly). 只是一个显示其工作原理的示例(编辑:添加了$compile指令, buttonClick才能正常工作)。

 angular.module('selectExample', []) .controller('ExampleController', ['$scope', function($scope) { const ItemsInfo = [ { ITEM_ID: 'a1', COMMENT: 'comment a1', PRIORITY: 0 }, { ITEM_ID: 'b1', COMMENT: 'comment b1', PRIORITY: 1 }, { ITEM_ID: 'c1', COMMENT: 'comment c1', PRIORITY: 2 }, ]; let content = ''; for (let d = 0; d < ItemsInfo.length; d++) { content += '<tr> <td> <label for="lblPriority">Item Priority </label> </td> '; content += ' <td> <input type="text" id="inpItemPRIORITY_' + d + '" value=" ' + ItemsInfo[d].PRIORITY + ' " /> </td> </tr>'; content += '<tr> <td> <label for="lblItemComment">Item Comment</label></td> '; content += ' <td> <input type="text" id="inpItemCOMMENT_' + d + '" value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>'; // Save Item content += '<tr> <td> <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> '; } $scope.htmlContent = content; $scope.buttonClick = function (index) { let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ), cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) ); console.log(prty.val() + " " + cmnt.val()); }; }]) .directive('bindHtmlCompile', ['$compile', function ($compile) { return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { element.html(value); $compile(element.contents())(scope); }); } }; }]); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-sanitize.min.js"></script> <div ng-app="selectExample" ng-controller="ExampleController"> <table bind-html-compile="htmlContent"></table> </div> 

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

相关问题 如何从动态生成的文本框中保存数据? - How to save data from dynamically generated textboxes? 如何从jsp代码中动态添加的文本框中获取数据 - how to fetch data from dynamically added textboxes from jsp code 无法从JavaScript中动态创建的文本框中获取值 - Cannot fetch values from dynamically created textboxes in javascript 如何在单击按钮时在 angularjs 中动态添加输入字段? - how to add input fields dynamically in angularjs on button click? 如何将动态创建的按钮单击事件绑定到 javascript 中动态创建的输入? - how to bind dynamically created button click events to dynamically created input in javascript? 如何使用JQuery或javascript从动态创建的按钮获取单击事件对象数据 - How to get click event object data from dynamically created button using JQuery or javascript 使用 ng-for 从 Angular 中动态创建的文本框中获取数据 - Get Data from Dynamically Created textboxes in Angular using ng-for 如何使用JavaScript在按钮单击中动态添加文本框 - How to dynamically add textboxes in a button click using javascript 如何使用codeiginter从动态创建的表中的数据库中获取数据 - how to fetch the data from database in dynamically created table using codeiginter 如何从jQuery中动态创建的元素中仅选择文本框? - How to select only textboxes from a bunch of dynamically created elements in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM