2011-11-21 95 views
1

我有以下javascript函数,它创建一个新行,其中包含按钮作为单元格项类型的不同单元格。那么我怎么才能在这里实现按钮功能的onClick如何在javascript中实现onclick对象?

我的代码:

var propertyCell = row.insertCell(1); 
    var propertyName = document.createElement("input"); 
    propertyName.type = "text"; 
    propertyName.size = "6"; 
    propertyName.id = "pName"+rowCount; 

    //alert(propertyName.size); 
    propertyCell.appendChild(propertyName); 
var image1Cell = row.insertCell(6); 
    var image1 = document.createElement("input"); 
    image1.type = "button"; 
    image1.value = "Camera"; 
    image1.size = "6"; 
    image1.id = "image1"+rowCount; 
    //image1.onclick = function(){sdfkvk(dh,sdd)} 

    image1Cell.appendChild(image1); 

回答

2

你可以试试下面的代码:

image1.onclick = function(){alert(document.getElementById("pName"+rowCount).value)}; 
0

你得到使用的document.getElementById文本域的元素作为行数是一样的按钮的值,你可以做一个字符串拆分得到的数量。然后我只提醒价值,但一旦你有了价值,你就可以做你喜欢的事情。

image1.onclick = function() { 
     alert(document.getElementById("pName" + this.id.split('e1')[1]).value); 
    }; 
+0

this.id.split( 'E1')[1]应该image1.id返回的行数号,{}这将指向按钮。 – kamui

+0

是的..即使这工作正常:)谢谢你 – Smitha