在这里,我正在使用 jQuery 表,请帮助我。如何捕获 Id 值并将此值绑定到下一个函数
success: function (data) {
var $table = $('<table></table>').addClass('table table-responsive table-striped table-bordered');
var $header = $('<thead><tr><th>EmpId</th><th>EmpName</th><th>Email</th><th>Action</th></tr><thead/>');
$table.append($header);
$.each(data, function (i, value) {
var $row = $('<tr></tr>');
$row.append($('<td/>').html(value.Emp_Id));
$row.append($('<td/>').html('<a id="Hello" href="#" value='+value.Emp_Id+'>Edit</a>'))
$table.append($row);
});
$('#Div1').html($table);
}
在这里,我想绑定该值-
$('body').on('click', '#Hello', function (e) {
var page2 = $(this).valueOf();
console.log(page2);
});
你需要像下面那样做:-
$(document).on('click', 'tr td a', function (e) { //bind click on link
e.preventDefault(); //stop page refresh and page url change
var page2 = $(this).attr('value'); //get link value
console.log(page2);
});
工作示例:-
$(function(){
var data=[{Emp_Id:1,Emp_Name:'alive',Email:'[email protected]'},{Emp_Id:2,Emp_Name:'to',Email:'[email protected]'},{Emp_Id:3,Emp_Name:'die',Email:'[email protected]'}];
var $table = $('<table></table>').addClass('table table-responsive table-striped table-bordered');
var $header = $('<thead><tr><th>EmpId</th><th>EmpName</th><th>Email</th><th>Action</th></tr><thead/>');
$table.append($header);
$.each(data, function (i, value) {
var $row = $('<tr></tr>');
$row.append($('<td/>').html(value.Emp_Id));
$row.append($('<td/>').html(value.Emp_Name));
$row.append($('<td/>').html(value.Email));
$row.append($('<td/>').html('<a href="#" value='+value.Emp_Id+'>Edit</a>')); // no need of creating repetitive id="Hello"
$table.append($row);
});
$('#Div1').html($table);
})
$(document).on('click', 'tr td a', function (e) { //bind click on link
e.preventDefault(); //stop page refresh and page url change
var page2 = $(this).attr('value'); //get link value
console.log(page2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Div1"></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句