Как создать нужное количество тегов таблицы
Смысл такой, делаю запрос в базу обратно получаю массив
из массива выбираю три поля fam name otch и добавляю их у уже созданную таблицу... вот пример <table width="100" border="0" cellspacing="10" cellpadding="10"> <tr> <th scope="col" id="fam0"> </th> <th scope="col" id="name0"> </th> <th scope="col" id="otch0"> </th> </tr> <tr> <td id="fam1"> </td> <td id="name1"> </td> <td id="otch1"> </td> </tr> </table> <script> function clk (event) { var x = event.target.value; alert(x); } function clk(obj) { var x = event.target.value; $.ajax({ type: "POST", url: "http://localhost/football/poisk.php", data: ({x:x}), dataType: 'json', beforesend: alert("Отправляемые данные a=" +x), success: function(data){ $.each(data, function(index,a){ // index -> eaeie ii n?aoo iauaeo a data, a -> data[ index ] for(var i in a ) { // i -> Id_p, a[ p ] -> qwerty if(i === "name"){ $("#name" + index).empty(a[i]); $("#name" + index).append(a[i]); };// cae?uoea if if(i === "fam"){ $("#fam" + index).empty(a[i]); $("#fam" + index).append(a[i]); };// cae?uoea if if(i === "otch"){ $("#otch" + index).empty(a[i]); $("#otch" + index).append(a[i]); };// cae?uoea if }; // cae?uoea for // alert("Iiia? : "+ index+"\n\n ?cue : "+ a[i]); }); // cae?uoea $.each(data, function(index,a){ }, // cae?uoea success: function(data){ }); }; </script> Как мне создать столько строк таблицы сколько у меня будет index, |
Цитата:
var x = event.target.value; далее получаю массив из которого нахожу нужные поля fam name otch и присваиваю индекс первая фамилия в массиве имеет индекс 0 вторая 1 и т д fam0 fam1 итд далее я помещаю все это в ячейки таблицы которые уже созданны <table width="100" border="0" cellspacing="10" cellpadding="10"> <tr> <th scope="col" id="fam0"> </th> <th scope="col" id="name0"> </th> <th scope="col" id="otch0"> </th> </tr> <tr> <td id="fam1"> </td> <td id="name1"> </td> <td id="otch1"> </td> </tr> </table> а я хочу чтобы таблица создавалась сама с таким количеством строк сколько у меня фамилий(индексов) |
Как вариант...
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <!-- <link rel="stylesheet" type="text/css" href="tmp.css" /> --> <style type="text/css"> </style> <script type="text/javascript"> var a=[ {'fam': 'Иванов', 'name': 'Иван', 'otch': 'Иванович'}, {'fam': 'Петров', 'name': 'Пётр', 'otch': 'Петрович'} ]; $(document).ready(function(){ $("button").click(function() { var i; var str; for (i=0; i<a.length; i++) { str='<tr>'; str+='<td>'; str+=a[i].fam str+='</td>'; str+='<td>'; str+=a[i].name str+='</td>'; str+='<td>'; str+=a[i].otch str+='</td>'; str+='</tr>'; $('#data').append(str); }; }); }); </script> </head> <body> <table width="100" border="1" cellspacing="10" cellpadding="10"> <thead> <tr> <th scope="col" id="fam0"> </th> <th scope="col" id="name0"> </th> <th scope="col" id="otch0"> </th> </tr> </thead> <tbody id='data'> <tr> <td id="fam1"> </td> <td id="name1"> </td> <td id="otch1"> </td> </tr> </tbody> </table> <button>Add</button> </body> </html> |
Часовой пояс GMT +3, время: 15:38. |