2017-08-22 235 views
0

即时尝试在这里做的是获取单击函数上的表的id,然后当我单击删除按钮时,它会得到该id并将其删除。将变量从一个Jquery函数传递给另一个

$("table tr").click(function() { 
var id; 
    $(this).removeClass('table-hover'); 
$(this).css("background-color", "#4682B4"); 
id = $(this).find(".id").html(); 
alert("Are you sure you want to delete data with id = " + id); //using just to check if its getting the id of the row 


}); 


$('.btnDelete').click(function() { 


$.ajax({ 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    type: 'POST', 
    url: 'EmployeeDelete', 
    data: JSON.stringify({ id: id }), 
    success: function (data) { 
       } 
}); 
}); 

我在做什么错在这里

+0

你存储的ID表的TR它被认为是固定的吗? –

+0

即时获取tr的id并将其保存到var id –

回答

0

我假设你对下面每一行按键的HTML数据属性,这是你想要的行 - 使用变量的作用域两种功能 访问删除。获取行I的ID。

表的每一行都有一个唯一的ID。下面的Jquery函数使用“最接近”的方法并获取ID。从那里你可以删除它。

下面是代码,没有测试,但它应该:-)

var button = $('.MyButtonClass'); $(button).on('click', function() { 
     var rowID = $(this).closest("tr").attr("id"); 
     //Delete the row }); 
+0

不,我在顶部有一个按钮我做什么是当我点击一行它将获得id并突出显示该行然后当我将点击删除按钮,它将删除该行,这就是为什么我需要将ID从一个函数传递到另一个函数 –

+0

然后,我会从Zim84的全局变量的解决方案。 :-)你可以将它作为session.storage保存在浏览器中。当用户单击该行时,该id将保存在session.storage中。单击该按钮可以获取会话存储并删除该行。检查:HTTPS://www.w3schools.com/html/html5_webstorage.asp – Troels

1

你的变量ID仅仅是函数内有效。尝试要么 - 使用tr标签

+0

非常感谢你 –

1
$("table tbody tr").click(function (event) { 


$(this).removeClass('table-hover'); 
$(this).css("background-color","#4682B4"); 
if (typeof(Storage) !== "undefined") 
{ 
    sessionStorage.empID = $('td.id', this).html(); 
} 
else{ 
    document.getElementById("result").innerHTML = "Sory, browser does not support web storage"; 
    } 

$.ajax({ 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    type: 'POST', 
    url: 'getID', 
    data: JSON.stringify({ empID: sessionStorage.empID }), 
    success: function (data) { 
    }, 
    failure: function (response) 
    { 
     $('#result').html(response); 
    } 
}); 
}); 

工作用的sessionStorage感谢大家

相关问题