[英]Get the row value when the button inside the particular row is clicked
我有一個數據表,單擊按鈕后會在其中添加新行。 在該行中,我有一個按鈕,單擊,我需要獲取所單擊的行的值。 我已經嘗試過使用此代碼,但不會觸發。
$(".add-beneficiary").click(function (e) {
e.preventDefault();
alert("Button Clicked");
});
這段代碼是在單擊添加行時在表中添加文本框(這很完美)
$('#addRow').on('click', function () {
debugger;
ctr++;
var _beneficiaryname = "_beneficiaryname" + ctr;
var _beneficiarytype = "_beneficiarytype" + ctr;
var _beneficiarypercent = "_beneficiarypercent" + ctr;
var _addbtn = "_addbtn" + ctr;
var newTr = '<tr><td><input type="text" class="nr" id=' + _beneficiaryname + ' /></td><td><select class="form-control relationshiptype" id=' + _beneficiarytype + '>' + selectoption + '</select> </td><td><input type="text" id=' + _beneficiarypercent + ' /></td><td> <button type="submit" class="btn btn-primary add-beneficiary" id=' + _addbtn + ' >Add Beneficiary</button></td></tr>';
//<td><div class=ibox-tools><a class=dropdown-toggle data-toggle=dropdown href=#><i class=fa fa-wrench></i></a><ul class=dropdown-menu dropdown-user><li><a href=# onclick=AddBeneficiaryFunction()>Add</a></li></ul></div></td>
$('#jsontable').append(newTr);
});
我不知道如何獲取行值?
如果您只需要單擊按鈕的tr的html,請嘗試以下操作:
$('#jsontable').on('click', '.btn', function()
{
alert($(this).closest('tr').html());
});
如果需要單擊的輸入值:
$('#jsontable').on('click', '.btn', function()
{
alert($(this).closest('tr').find('.nr').val());
});
我希望你有主意嗎? 對於生成的按鈕,您需要在單擊操作上另外指定按鈕標識符,並且選擇器必須是“穩定(未生成)” DOM元素。 就像人們所說的那樣,這被稱為事件委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.