简体   繁体   English

如何从codeigniter日历的ajax成功结果获取元素的属性?

[英]How to get attr of element from ajax success result of codeigniter calendar?

I going to work with codeigniter calendar for showing the events. 我将使用codeigniter日历来显示事件。 By default, when I am generate the calender and showing each events, there is no problem. 默认情况下,当我生成日历并显示每个事件时,没有问题。 Each events can shown on the bootstrap modal depend on the day that (clicked) dan pass to the my controller function. 每个事件可以显示在引导程序模式上,具体取决于(单击)dan传递给my控制器功能的日期。 The problem is when I click the prev or next link by using jquery ajax to load the prev or next month, after the month changed, and then I click on a day, the attr value can't catch by jquery $(this) function. 问题是当我使用jquery ajax单击prev或next链接来加载prev或下个月时,当月份改变后,然后单击一天,则jquery $(this)函数无法捕获attr值。 There is my snipped code : 有我的代码片段:

CI calendar template snipped : CI日历模板已删除:

{cal_cell_content}<div data-toggle="modal" data-target=".mymodal" class="detail" val="{day}"><span class="myclass">{day}</span><span class="d{day}">{content}</span></div>{/cal_cell_content}

function to load calendar : 加载日历的功能:

public function show_cal_ajax($yr = NULL, $mth = NULL)
    {
        $this->load->model('m_cal');
        $this->load->library('calendar', $this->_calconfig()); 
        $year = $month = "";
        if($yr != NULL && $mth != NULL)
        {
            $month = $mth;
            $year = $yr;
        }
        else
        {
            $month = date('m');
            $year = date('Y');
        }

        $numEvent   = $this->m_cal->numOfEvent($month, $year);
        $data           = array(
                            'cal_data' => $this->calendar->generate($year, $month, $numEvent)
                        );
        echo $data['cal_data'];
    }

and the jquery ajax for load the event detail : 和jquery ajax用于加载事件详细信息:

$(".detail").on('click',function(){
        var day = $(this).attr('val');

        $.ajax({
            type: 'POST',
            dataType: 'html',
            url: <?php echo "'".base_url('view/show_event_detail')."'"; ?>,
            data:{<?php echo "y: $year, m: $month";?>, d: day}, 
            success: function( data ) {
                $( ".modal-content").html(data);
            },
            error: function(){
                alert('Error: ajax request error.');
            } 
        });
});

So, after the calendar changed by clicking the prev or next link, the jquery detail function not work, I guess that the $(this).attr('val') have not a value because the calendar data/month was changed by ajax request and append it on the #calendar on the html.. anyone can help me how to get the attr value although the month was changed... 因此,在通过单击上一个或下一个链接更改日历后,jQuery详细信息功能不起作用,我猜$(this).attr('val')没有值,因为日历数据/月已被ajax更改请求并将其附加到html的#calendar上。尽管月份已更改,任何人都可以帮助我如何获取attr值...

btw, the full code can be found here : http://pastebin.com/VgydABai thanks... 顺便说一句,完整的代码可以在这里找到: http : //pastebin.com/VgydABai谢谢...

Add the year month and day to your template then use the individual values for the post 将年月日添加到模板中,然后使用帖子的各个值

class="detail" data-day="{day}" data-month="{month}" data-year="{year}"


var caldata = {};
$(document).on('click', ".detail", function() {
    caldata["y"] = $(this).attr('data-year');
    caldata["m"] = $(this).attr('data-month');
    caldata["d"] = $(this).attr('data-year');

    $.ajax({
        type: 'POST',
        dataType: 'html',
        url: <?php echo "'".base_url('view/show_event_detail')."'"; ?>,
        data: caldata, 
        success: function( data ) {
            $( ".modal-content").html(data);
        },
        error: function(){
            alert('Error: ajax request error.');
        } 
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM