2013-06-28 59 views
0

在我的asp.net形式,錶行我補充細胞內這些細胞中的我像一個屬性添加文本框 -查找特定行屬性的jQuery

string residentId = (new GUID()).ToString(); 
string houseId = (new GUID()).ToString(); 

HtmlTableRow detailRow = new HtmlTableRow(); 
detailRow.Attributes.Add("residentId", residentId); 
detailRow.Attributes.Add("houseId", houseId); 

HtmlTableCell dataCell = new HtmlTableCell(); 

TextBox tb = new TextBox(); 
tb.Attributes.Add("componentname", "tbMoveInDate"); 

tb.Attributes.Add("onchange", "updateMoveInDate()"; 

cell.Controls.Add(tb); 
detailRow.Cells.Add(dataCell); 

與100+行的possiblility,所有當然有不同的ID。

我在JavaScript中

我有這個功能 -

function updateMoveInDate() { 
    var MoveInDateEle = $("[componentname*='tbMoveInDate']"); 
} 

在這一點上MoveInDateEle是所有的這些文本框的表內的收集和

var currentRow = $("[componentname*='tbMoveInDate']").parent().parent(); 

給了我所有的行。但不是我的特定行。

如何獲得我正在使用的特定文本框以及與該控件關聯的特定居民ID和房屋ID?

+0

你點擊你正在使用或什麼'工作with'意味着行?文本框會被重點關注嗎?你介意多解釋一下。謝謝 – edhedges

回答

1

你可以做

tb.Attributes.Add("onchange", "updateMoveInDate(this)"; 

而且

function updateMoveInDate(txt) { 
    var $txt = $(txt); 
    var $row = $txt.closest('tr'); 
    var residentId = $row.attr('residentId'); 
    var houseId = $row.attr('houseId'); 
} 
+0

完美。謝謝! – duckmike

2

修改C#這樣的代碼:

tb.Attributes.Add("onchange", "updateMoveInDate(this)"; 

和JS功能,如:

// obj here refers to the current textbox in the scope 
// on which the on-change event had occurred... 
function updateMoveInDate(obj) { 
    var currentRow = $(obj).closest('tr'); 
} 
+0

@edhedges這是因爲OP使用'$(「[componentname * ='tbMoveInDate']」)'引用所有文本框,但我們在這裏使用了'this'關鍵字,它引用了當前作用域中的元素,而不是完整的收集。 –

0

試試這個

tb.Attributes.Add("onchange", "updateMoveInDate(this)"; 
代碼

背後

string residentId = (new GUID()).ToString(); 
    string houseId = (new GUID()).ToString(); 

    HtmlTableRow detailRow = new HtmlTableRow(); 
    detailRow.Attributes.Add("residentId", residentId); 
    detailRow.Attributes.Add("houseId", houseId); 

    HtmlTableCell dataCell = new HtmlTableCell(); 

    TextBox tb = new TextBox(); 
    tb.Attributes.Add("componentname", "tbMoveInDate"); 

    tb.Attributes.Add("onchange", "updateMoveInDate(this)"; 

    cell.Controls.Add(tb); 
    detailRow.Cells.Add(dataCell); 

的jQuery:

function updateMoveInDate(args) { 
    var MoveInDateEle = $(args).closest('tr'); 

}