jquery 迭代数组并匹配 html table 中的值并填充列
jquery interate array and match to values in html table and fill in column
我想通读数组。匹配员工编号table,匹配时在table填写奖金值
<script type="text/javascript" src="jquery.js"></script>
<script>
$( document ).ready(function() {
// note that employee may get 0,1, or more bonuses
myArray =
{"employees": [
{"employee": "1231", "bonus":100},
{"employee": "1232", "bonus":200},
{"employee": "1233", "bonus":300}
{"employee": "1233", "bonus":33}
]
}
$.each(myArray.employees, function(index) {
console.log(this.employee);
// having problem here...
$("#myTable:has(td:contains(this.employee)").each(function () {
console.log("found");
// then write to 2nd cell in table next to employee#
// assume that employee number in table is unique
});
});
});
</script>
<table id='myTable' border='1px'>
<tr>
<th>Employee</th>
<th>Bonus</th>
</tr>
<tr>
<td>1232</td>
<td> </td>
</tr>
<tr>
<td>1231</td>
<td> </td>
</tr>
<table>
我每次都会循环table
$( document ).ready(function() {
// note that employee may get 0,1, or more bonuses
myArray =
{"employees": [
{"employee": "1231", "bonus":100},
{"employee": "1232", "bonus":200},
{"employee": "1233", "bonus":300},
{"employee": "1233", "bonus":33}
]
};
$.each(myArray.employees, function(index) {
var emp=this.employee;
var bonus=this.bonus;
$('#myTable td').each(function(){
if($(this).text()==emp){
console.log('found');
$(this).next().text(bonus);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='myTable' border='1px'>
<tr>
<th>Employee</th>
<th>Bonus</th>
</tr>
<tr>
<td>1232</td>
<td> </td>
</tr>
<tr>
<td>1231</td>
<td> </td>
</tr>
<table>
我想通读数组。匹配员工编号table,匹配时在table填写奖金值
<script type="text/javascript" src="jquery.js"></script>
<script>
$( document ).ready(function() {
// note that employee may get 0,1, or more bonuses
myArray =
{"employees": [
{"employee": "1231", "bonus":100},
{"employee": "1232", "bonus":200},
{"employee": "1233", "bonus":300}
{"employee": "1233", "bonus":33}
]
}
$.each(myArray.employees, function(index) {
console.log(this.employee);
// having problem here...
$("#myTable:has(td:contains(this.employee)").each(function () {
console.log("found");
// then write to 2nd cell in table next to employee#
// assume that employee number in table is unique
});
});
});
</script>
<table id='myTable' border='1px'>
<tr>
<th>Employee</th>
<th>Bonus</th>
</tr>
<tr>
<td>1232</td>
<td> </td>
</tr>
<tr>
<td>1231</td>
<td> </td>
</tr>
<table>
我每次都会循环table
$( document ).ready(function() {
// note that employee may get 0,1, or more bonuses
myArray =
{"employees": [
{"employee": "1231", "bonus":100},
{"employee": "1232", "bonus":200},
{"employee": "1233", "bonus":300},
{"employee": "1233", "bonus":33}
]
};
$.each(myArray.employees, function(index) {
var emp=this.employee;
var bonus=this.bonus;
$('#myTable td').each(function(){
if($(this).text()==emp){
console.log('found');
$(this).next().text(bonus);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='myTable' border='1px'>
<tr>
<th>Employee</th>
<th>Bonus</th>
</tr>
<tr>
<td>1232</td>
<td> </td>
</tr>
<tr>
<td>1231</td>
<td> </td>
</tr>
<table>