Home > Web Front-end > JS Tutorial > body text

How to dynamically add tr and td of a table in ajax

php中世界最好的语言
Release: 2018-06-05 15:45:56
Original
1857 people have browsed it

This time I will show you how ajax can dynamically add tr and td of a table, and what are the precautions for how ajax can dynamically add tr and td of a table. The following is a practical case, let's take a look.

Function: ajax gets the background return data and dynamically adds tr/td to the table

html part:

Copy after login

ajax part:

var year = $('#year').val();//下拉框数据
var province= $('#province').val();//下拉框数据
$('table tbody').html('');
$.ajax({
  url:"/Plan/sendJson.html",
  type:"get",
  data:{
     'year':year,
     'province':province
  },
  datatype:'json',
  success:function(data){
     switch(data.msg)
     {
        case '0':
            $('table tbody').prepend('暂无数据');break;
        case '1':
            $.each(data.data,function(i,n){
var $tr = $(""+
""+n.year+""+
""+n.province+""+
"");
var $table = $('table tbody');
$table.append($tr);
});
}
}
})
Copy after login

php background (thinkPHP processing):

$year = I('get.year');
$province = I('get.province');
$condition = array();
$year && $condition = array('eq',$year);
$province && $condition = array('eq',$province);
$dataList = M('Plan')->where($condition)->select();
if(false != $dataList){
  $data['msg'] = '1';
  $data['data'] = $dataList;
  echo json_encode($data);
  exit;
}else{
  $data['msg'] = '0';
  $data['data'] = '';
  echo json_encode($data);
  exit;
}
Copy after login

I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!

Recommended reading:

Using try-catch statements and error types in JS

The above is the detailed content of How to dynamically add tr and td of a table in ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!