获取button自身id ajax发送后台 并接收返回数据并处理

<script type="text/javascript">
    
            //$('.addpet').click(function () {
            //var form = new FormData(document.getElementById("formL"));
            $(document).ready(function(){
                $('td > button').click(function(){
                    //alert($(this).attr("id"));
                    //alert($(this).html())
                var content = $(this).attr("id");
                if(content){
            // var content = $('.addpet').val();
            console.log(content);
                
     // return;
            $.ajax({
                url: "/admin/Contract/addpet",
                 type: "get",
               
                 data: {'id':content},
                 dataType: "json",
 
                success: function (res) {
                    console.log(res)
                    $(".vpets table thead").remove();
                    $(".vpets table tbody").remove();
                    var tempHtml = "<thead><tr><th>宠物名</th>"
                                    +"<th>品种</th><th>性别</th><th>绝育</th>"
                                    +"<th>生日</th><th>毛色</th><th>身高</th>"
                                    +"<th>体重</th></tr></thead>";
                    $.each(res.data,function(n,value){
                        tempHtml += "<tbody><tr><td>"+value.pet_name+"</td>"
                                    +"<td>"+value.pet_type+"</td>"
                                    +"<td>"+value.pet_sex+"</td>"
                                    +"<td>"+value.sterilization+"</td>"
                                    +"<td>"+value.birthday+"</td>"
                                    +"<td>"+value.color+"</td>"
                                    +"<td>"+value.height+"</td>"
                                    +"<td>"+value.weight+"</td>"
                                    +"<td><div class='actions'>"
                                    +"<a href='编辑产品分类.html' class='btn btn-sm bg-success-light mr-2'>"
                                    +"<i class='fe fe-pencil'></i>"
                                    +"<font style='vertical-align: inherit;'>"
                                    +"<font style='vertical-align: inherit;'>编辑"
                                    +"</font>"
                                    +"</font>"
                                    +"</a><a href='' class='btn btn-sm bg-danger-light'>"
                                    +"<i class='fe fe-trash'></i><font style='vertical-align: inherit;'>"
                                    +"<font style='vertical-align: inherit;'> 删除"
                                    +"</font></font></a></div></td><tr>"
                                    +"</tbody>";
                        //console.log(value.pet_name);
                    })
                    $(".vpets table").append(tempHtml);
                    
                    if (res.status == 0) {
                        alert(res.msg);
                        return;
                    }
                    //alert(res);
                    //location.href = res.url;
                    //return;
                    },
                error:function(){
                    console.log("请求失败")
                    },
                    timeout:2000,
                });
                //success:function(res){
                    //console.log(res);
                    
                //},
                //error:function(){
                    //console.log("请求失败")
                    //},
                    //timeout:2000,
                //});
            }
            })
             })
        </script>

猜你喜欢

转载自www.cnblogs.com/wangyinqiuqian/p/12016287.html
今日推荐