我想在单独的函数中获取Addmission id字段。当用户点击按钮时。
$(document).ready(function () {
$.each(data.student, function (i, item){
trHTML += '<tr>'+
'<td>'+data.student[i]['admission_no']+'</td>' +
'<td>'+ data.student[i]['fullname']+'</td>' +
'<td>'+data.student[i]['gender']+ '</td>' +
'<td>'+data.student[i]['dob']+'</td>' +
'<td>'+data.student[i]['class_no']+data.student[i] ['class_id']+'</td>'+
'<td><button id="selectStu" name="selectStu" ' +
'value="'+data.student[i]['admission_no']+'">View</button> </td>'+
'<td></td>
</tr>';
});
//i get the table.
//1000 Name1 Male 2A View
//1001 Name2 Male 2A View
//1002 Name3 Male 2A View
$('#location').html(trHTML);
$('#showByClass').show();
$('#selectStu').click(function (e){
e.preventDefault();
//I need to get clicked "data.student[i]['admission_no']" Here
});
});
//这是我的数据数组,
{&#34;学生&#34;:[{&#34;全名&#34;:&#34; dfsdf fdsfsdfsf&#34;,&#34;性别&#34;:&#34;男&#34 ;,&#34; DOB&#34;:&#34; 2017年1月18日&#34;&#34; admission_no&#34;:&#34; 1000&#34;&#34;类标识码&#34; :&#34; A&#34;&#34; class_no&#34;:&#34; 2&#34;}, {&#34;全名&#34;:&#34; dfsdf fdsfsdfsf&#34;,&#34;性别&#34;:&#34;男&#34;,&#34; dob&#34;:&#34 ; 2017年1月18日&#34;&#34; admission_no&#34;:&#34; 1001&#34;&#34;类标识码&#34;:&#34; A&#34;&#34; class_no&#34;:&#34; 2&#34;}, {&#34;全名&#34;:&#34; dfsdf fdsfsdfsf&#34;,&#34;性别&#34;:&#34;男&#34;,&#34; dob&#34;:&#34 ; 2017年1月18日&#34;&#34; admission_no&#34;:&#34; 1003&#34;&#34;类标识码&#34;:&#34; A&#34;&#34; class_no&#34;:&#34; 2&#34;}, {&#34;全名&#34;:&#34; dfsdf fdsfsdfsf&#34;,&#34;性别&#34;:&#34;男&#34;,&#34; dob&#34;:&#34 ; 2017年1月18日&#34;&#34; admission_no&#34;:&#34; 1005&#34;&#34;类标识码&#34;:&#34; A&#34;&#34; class_no&#34;:&#34; 2&#34;}]}
答案 0 :(得分:0)
您可以使用$(this).val()
获取点击的按钮值,如下所示
注意:
1)您可以使用课程.selectStu
代替#selectStu
2)每个循环都有错误。你应该传递第一个索引,如data[0]["student"]
$(document).ready(function () {
var data = [{"student":[{"fullname":"dfsdf fdsfsdfsf","gender":"Male","dob":"2017-01-18","admission_no":"1000","class_id":"A","class_no":"2"}, {"fullname":"dfsdf fdsfsdfsf","gender":"Male","dob":"2017-01-18","admission_no":"1001","class_id":"A","class_no":"2"}, {"fullname":"dfsdf fdsfsdfsf","gender":"Male","dob":"2017-01-18","admission_no":"1003","class_id":"A","class_no":"2"}, {"fullname":"dfsdf fdsfsdfsf","gender":"Male","dob":"2017-01-18","admission_no":"1005","class_id":"A","class_no":"2"}]}];
var trHTML ="";
$.each(data[0]["student"], function (i, item){
trHTML += '<tr>'+
'<td>'+item['admission_no']+'</td>' +
'<td>'+ item['fullname']+'</td>' +
'<td>'+ item['gender']+ '</td>' +
'<td>'+ item['dob']+'</td>' +
'<td>'+ item['class_no']+item['class_id']+'</td>'+
'<td><button name="selectStu" class="selectStu" ' +
'value="'+item['admission_no']+'">View</button> </td>'+
'</tr>';
});
$('tbody').html(trHTML);
// $('#showByClass').show();
$('.selectStu').click(function (e)
{
e.preventDefault();
alert($(this).val());
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1px" >
<thead>
<tr>
<th>Adm.No</th><th>F.name</th><th>gender</th><th>dob</th><th>class</th><th>action</th>
</tr>
<thead>
<tbody >
</tbody>
</table>
&#13;