在ajax成功执行并加载了特定div之后,内容消失了

时间:2019-04-18 10:23:17

标签: javascript php jquery codeigniter

我正在制作活动日历。添加事件后,成功插入数据后将仅加载日历的单元格。添加事件很好,但是当div加载时,内容消失了。这是我的代码:

* Ajax *

$('.savedata').on('click', function () {
                // alert('savedata');
                var testchecked = $('#testchecked').val();
                var testingcheck = $('#testingcheck').val();
                $.ajax({
                    type : "POST",
                    url  : "<?php echo site_url()?>/CalendarController/savedata",
                    // dataType : "JSON",
                    data : { testchecked : testchecked, testingcheck : testingcheck },
                    success: function(data){
                        // alert('success');
                        //console.log('asd '+data);

                        $('#events-modal').modal('hide');
                        // cal-month-box
                        // $(".cal-month-day").append(JSON.stringify(data));
                        // $(".cal-month-day").load(window.location + " .cal-month-day");
                        // $('.cal-month-day').prepend(_newData);
                        $(".cal-month-day").load(location.href + " .cal-month-day");
                    },
                    error : function(data, ajaxOptions, thrownError) {
                        console.log(data.status);
                    }
                    });
                });

*添加数据之前*

enter image description here

*添加事件*

enter image description here

*这是HTML *

enter image description here

*控制器*

public function savedata()
    {
        $this->load->view('calendarView');

        $this->em->form_insert();

    }

*型号*

function form_insert()
    {
        $abc            = $this->input->post('testchecked');

        $array = array('id' => $abc);
        $this->db->select("*");
        $this->db->where($array);
        $query = $this->db->get('dev_adkt_events_type');
        $response = $query->result_array();
        foreach($response as $res){
            $title = $res['id'];
        }


        $url            = 'URL';
        $class          = $this->input->post('testchecked');
        $start_date     = $this->input->post('testingcheck');
        $end_date       = $this->input->post('testingcheck');

        $data = array(
            'title'     => $title,
            'url'       => $url,
            'class'     => $class,
            'start_date'=> $start_date ,
            'end_date'  => $end_date 
        );

        // $this->db->insert('event',$data);
        $result = $this->db->insert('event',$data);
        return $result;

    }

希望您能帮助我。预先谢谢你。

*显示日历*

控制器

public function index() {
        $this->load->helper('url');
        // var_dump($postData);
        $result['eventcatresult'] = $this->em->geteventcategory();
        $result['data'] = $this->em->getevent();
        $result['data'] = $this->em->getnotes();

        $this->load->view('calendarView',$result);
    }

*型号*

function getevent()
    {
        $this->db->select("*");
        $query = $this->db->get($this->event);
        if ($query) {
            return $query->result();
        }
        return NULL;
    }

1 个答案:

答案 0 :(得分:0)

您最好更改此

$(".cal-month-day").load(location.href + " .cal-month-day");

对此

$("#calendar").load(location.href + " #calendar");

因为每个事件都有多个'cal-month-day',并且如果最后一个元素为空,则所有元素都将为空
最好刷新所有日历