简体   繁体   English

这个js代码中的错误是什么

[英]what is error in this js code

  var RowNumber = row.rowIndex;
  alert(RowNumber);
  var a = document.createElement('a');
  a.textContent = 'Remove';
  a.style.cursor = 'pointer';
  //a.href = "javascript: document.getElementById(tblId).deleteRow(0);"; 
  a.setAttribute("onclick","alert(RowNumber)");
  cell1.appendChild(a);

firebug say 'RowNumber is not defined' but i have defined RowNumber above. firebug说'RowNumber没有定义',但我已经定义了RowNumber。 Also alrting the RowNumber variable. 同时改变RowNumber变量。

And my complete function is = 我的完整功能是=

function addRowToTable(tblId,icOptionArray,leavRowFromBottom,selectedValue,selectedQuantity)
{

  var tbl = document.getElementById(tblId);
  var lastRow = tbl.rows.length - (leavRowFromBottom + 0);
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
//  var iteration = lastRow + 1;
  var row = tbl.insertRow(lastRow);

  //  cell 0
  var cell0 = row.insertCell(0);
  cell0.align='right';

  var el = document.createElement('label');
  el.textContent = 'IC Chips :';
  cell0.appendChild(el);

  //cell 1
  var cell1 = row.insertCell(1);


  var selector = document.createElement('select');
  selector.id = 'icchips';
  selector.name = 'icchips[]';
  selector.style.width = '200px';
  //alert(selector);

  for (var key in icOptionArray) 
  {   
      if(key == 'containsValue') continue;
      var option = document.createElement('option');
      if(key == selectedValue)
         option.selected = true;
      option.value = key;
      option.appendChild(document.createTextNode(icOptionArray[key]));
      selector.appendChild(option);
  }

  cell1.appendChild(selector);

  var space = document.createTextNode(' ');
     cell1.appendChild(space);  

  var lbl = document.createElement('label');
  lbl.textContent = 'Qty :';
  cell1.appendChild(lbl);  


  var selector = document.createElement('select');
  selector.id = 'additional_quantity';
  selector.name = 'additional_quantity[]';

  for (var key in QUANTITIES_ARR) 
  {   
      if(key == 'containsValue') continue;
      var option = document.createElement('option');
      if(key == selectedQuantity)
         option.selected = true;
      option.value = key;
      option.appendChild(document.createTextNode(QUANTITIES_ARR[key]));
      selector.appendChild(option);
  }

  cell1.appendChild(selector);  
  var space = document.createTextNode(' ');
  cell1.appendChild(space);

  var RowNumber = row.rowIndex;
  alert(RowNumber);
  var a = document.createElement('a');
  a.textContent = 'Remove';
  a.style.cursor = 'pointer';
  //a.href = "javascript: document.getElementById(tblId).deleteRow(0);"; 
  a.setAttribute("onclick","alert(RowNumber)");
  cell1.appendChild(a);

}

这样做

a.onclick = function(){ alert(RowNumber); };

The problem is that the local scope where RowNumber is defined is not the same as the scope wherein your onclick handler is called. 问题是定义RowNumber的本地范围与调用onclick处理程序的范围不同。 The solution proposed by @jancha fixes that. @jancha提出的解决方案解决了这个问题。 His use of a closure ensures the RowNumber in the handler is the same as the one in the local scope. 他使用闭包确保处理程序中的RowNumber与本地作用域中的RowNumber相同。

RowNumber is not defined in the scope of the created element, since your using jQuery you could use: RowNumber未在创建的元素的范围内定义,因为您可以使用jQuery:

var RowNumber = row.rowIndex;
alert(RowNumber);
var a = document.createElement('a');
a.textContent = 'Remove';
a.style.cursor = 'pointer';
//a.href = "javascript: document.getElementById(tblId).deleteRow(0);"; 
$(a).click(function(e){
  alert(RowNumber);
});

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

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