0
  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);

萤火虫说“未定义行号”,但我在上面定义了行号。还改变了 RowNumber 变量。

我的完整功能是=

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);

}
4

3 回答 3

1

这样做

a.onclick = function(){ alert(RowNumber); };
于 2012-07-13T12:42:48.943 回答
1

RowNumber问题是定义的本地范围与onclick调用处理程序的范围不同。@jancha 提出的解决方案解决了这个问题。他使用闭包确保处理程序中的 RowNumber 与本地范围内的相同。

于 2012-07-13T12:45:12.263 回答
0

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);
});
于 2012-07-13T12:46:13.033 回答