简体   繁体   English

计算动态创建的DOM中的值

[英]Calculating values in dynamically created DOM

I have created an html table that is dynamically created in the DOM. 我创建了一个在DOM中动态创建的html表。 It has 4 columns and two of the columns will have tetboxes that a user can enter values into and then will be subtracted as a value in the last column. 它有4列,其中两列将具有用户可以输入值的tetbox,然后将其作为最后一列中的值减去。 'Delta' = 'State Before' - 'State After'. 'Delta'='State Before' - 'State After'。 I am able to get the value when it is entered but not sure how to get the value of the other field to do the computation. 我可以在输入时获取值,但不知道如何获取其他字段的值来进行计算。

HTML HTML

    <table id="deptState" border="1">
    <tr>
        <th>Department</th>
        <th>State Before</th>
        <th>State After</th>
        <th>Delta</th>          
    </tr>
</table>

JAVASCRIPT JAVASCRIPT

   var deptArray = ["Human Resources", "Accounting and Finance", "IT", "Marketing", "Purchasing Department"];
var state = ["stateBefore", "stateAfter", "Delta"];

for(var x=0; x<deptArray.length; x++) {
    var html = "<tr>";
        html +="<td>"+deptArray[x]+"</td>";
        for(var y=0; y<state.length; y++) {         
            if (state[y] === "stateBefore")
            {
                html += "<td><input type='text' class='.nrm-text' id='"+deptArray[x]+state[0]+"'></td>";
            }else if (state[y] === "stateAfter") {
                html += "<td><input type='text' class='.nrm-text' id='"+deptArray[x]+state[1]+"'></td>";            
            }else if (state[y] === "Delta") {
                html += "<td><input type='text' id='"+deptArray[x]+state[2]+"'></td>";          
            }           
        }
      html += "</tr>";
      $("#deptState").append(html); 
  }

  var allTextBoxes = document.querySelector("#deptState");
  console.dir(allTextBoxes);
  allTextBoxes.addEventListener("change", function( e ){
    if (e.target.tagName === 'INPUT'){      
              alert("Value: "+document.getElementById(e.target.id).value);  
            console.log("Save data to SharePoint list");
    }
  })

It's easier to show. 它更容易展示。 Here's the jsfiddle: 这是jsfiddle:
In one of the rows, enter 9 in 'State Before' and 5 in 'State After'. 在其中一行中,在“State Before”中输入9,在“State After”中输入5。 'Delta' should then have 4 'Delta'应该有4

http://jsfiddle.net/isogunro/pq4uref9/ http://jsfiddle.net/isogunro/pq4uref9/

use a common class: 使用一个共同的类:

  var deptArray = ["Human Resources", "Accounting and Finance", "IT", 
  "Marketing", "Purchasing Department"];
  var state = ["stateBefore", "stateAfter", "Delta"];

 for(var x=0; x<deptArray.length; x++) {
var html = "<tr>";
    html +="<td>"+deptArray[x]+"</td>";
    for(var y=0; y<state.length; y++) {         
        if (state[y] === "stateBefore")
        {
            html += "<td><input type='text' class='nrm-text state-before' id='"+deptArray[x]+state[0]+"'></td>";
        }else if (state[y] === "stateAfter") {
            html += "<td><input type='text' class='nrm-text state-after' id='"+deptArray[x]+state[1]+"'></td>";            
        }else if (state[y] === "Delta") {
            html += "<td><input type='text' class='delta' id='"+deptArray[x]+state[2]+"'></td>";          
        }           
    }
  html += "</tr>";
  $("#deptState").append(html); 
  }

  var allTextBoxes = document.querySelector("#deptState");
   console.dir(allTextBoxes);
   allTextBoxes.addEventListener("change", function( e ){
   if (e.target.tagName === 'INPUT'){      
          alert("Value: "+document.getElementById(e.target.id).value);  
        console.log("Save data to SharePoint list");
   }
 });

Now get the current rows delta value once we enter after State After : 现在,在State After之后输入时,获取当前行delta值:

$('.state-after').focusout(function(){
  var stateBefore=$(this).closest('tr').find('.state-before').val();
  var stateAfter=$(this).closest('tr').find('.state-after').val();
  var delta=(stateBefore-stateAfter);
  $(this).closest('tr').find('.delta').val(delta);
  console.log(delta);
});

Check the updated fiddle : 检查更新的小提琴

try this : 尝试这个 :

if (e.target.tagName === 'INPUT'){   
  // start 
  var rowNode = e.target.parentNode.parentNode;
  var rowInputs = rowNode.getElementsByTagName('input');
  var before = rowInputs[0].value || 0;
  var after = rowInputs[1].value || 0;
  if(before && after){
    rowInputs[2].value = before - after;
  } 
  // end
          alert("Value: "+document.getElementById(e.target.id).value);  
        console.log("Save data to SharePoint list");
}

http://jsfiddle.net/pq4uref9/10/ http://jsfiddle.net/pq4uref9/10/

I've made this a little more jqueried. 我已经把它变得更加j .. I've used jQueries on to hande events for dynamically added elements. 我已经使用jQueries来处理动态添加元素的事件。

Then we grab all the inputs from the row that has had a change and perform our operation 然后我们从已进行更改的行中获取所有输入并执行操作

 var deptArray = ["Human Resources", "Accounting and Finance", "IT", "Marketing", "Purchasing Department"]; var state = ["stateBefore", "stateAfter", "Delta"]; for(var x=0; x<deptArray.length; x++) { var html = "<tr>"; html +="<td>"+deptArray[x]+"</td>"; for(var y=0; y<state.length; y++) { if (state[y] === "stateBefore") { //alert(state[0]); html += "<td><input type='text' class='.nrm-text' id='"+deptArray[x]+state[0]+"'></td>"; }else if (state[y] === "stateAfter") { //alert(state[1]); html += "<td><input type='text' class='.nrm-text' id='"+deptArray[x]+state[1]+"'></td>"; }else if (state[y] === "Delta") { //alert(state[2]); html += "<td><input type='text' id='"+deptArray[x]+state[2]+"'></td>"; } } html += "</tr>"; //document.getElementById("#deptState").appendChild(html); $("#deptState").append(html); } $("#deptState").on("change", "input[type='text']", function(){ var inputs = $(this).closest("tr").find("input[type='text']"); var fldBefore = inputs[0]; var fldAfter = inputs[1]; var fldDelta = inputs[2]; if($(fldBefore).val().length > 0 && $(fldAfter).val().length > 0) { alert("Value: "+$(this).val()); console.log("Save data to SharePoint list"); $(fldDelta).val($(fldBefore).val() - $(fldAfter).val()) ; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="deptState" border="1"> <tr> <th>Department</th> <th>State Before</th> <th>State After</th> <th>Delta</th> </tr> </table> 

:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM