2015-06-29 34 views
0

我想通读数组。将员工编号与表格匹配,并在匹配表格中填写奖励值。jquery interate数组并匹配html表中的值并填写列

<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>&nbsp;</td> 
</tr> 
<tr> 
    <td>1231</td> 
    <td>&nbsp;</td> 
</tr> 
<table> 

回答

1

我将通过表中的每个时间循环

$(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>&nbsp;</td> 
 
</tr> 
 
<tr> 
 
    <td>1231</td> 
 
    <td>&nbsp;</td> 
 
</tr> 
 
<table>

+0

答案是了不起的! –

相关问题