我想用javascript调用控制器

时间:2015-09-21 07:26:53

标签: javascript codeigniter

在这里,我使用的是codeigniter。我想使用调用视图的javascript调用控制器方法。我想在一秒钟后调用view方法。我想从我的仪表板调用弹出窗口,所以我用控制器调用它。仪表板是我的" user_view.php" 。从" user_home.php"的角度来看,我想打电话给" break_alert.php"。

这是我的代码:

控制器:

function alert_breaktime()
{
     $this->load->view('break_alert');
}

查看:

user_view.php:

<script type="text/javascript">

var timer = setTimeout(function()
{ 
    alert('adsfadf');
    window.location.href = "<?php echo site_url('welcome/alert_breaktime');?>

},1000); 

</script>

break_alert.php:

<script src="<?=base_url()?>resource/js/jquery-2.1.1.min.js"></script>


<div class="modal-dialog">
    <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Add Activity</h4>
            </div>
            <?php $attributes = array('class' => 'bs-example form-horizontal'); echo form_open(base_url().'activity/add',$attributes); ?>
            <div class="modal-body">


                 <div class="form-group">
                    <label class="col-lg-2 control-label">Subject<span class="text-danger">*</span></label>
                    <div class="col-lg-10">
                        <input type="text" class="form-control holiday_name"  name="holiday_name" required>
                    </div>
                </div>

                <div class="form-group row">
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-phone"></i>&nbsp;Call</button>
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-group"></i>&nbsp;Meeting</button>
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-tasks"></i>&nbsp;Task</button>
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-flag"></i>&nbsp;Deadline</button>
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-envelope"></i>&nbsp;Email</button>
                    <button type="submit" class="btn btn-default col-md-2"><i class="fa fa-cutlery"></i>&nbsp;Lunch</button>

                </div>

                <div class="form-group">
                    <label class="col-lg-2 control-label">Date<span class="text-danger">*</span></label>
                    <div class="input-group date col-lg-10" id="datetimepicker5">
                        <input ng-model="dt" type='text' class="col-sm-3 form-control" value="" readonly data-date-format="YYYY/MM/DD"/>
                            <span class="input-group-addon">
                                <span class="fa fa-calendar"></span>
                            </span>
                    </div>
                </div>

                <div class="form-group">
                   <textarea class="textarea" id="description"></textarea>
                </div>

        <div class="modal-footer">
                    <a href="#" class="btn btn-default" data-dismiss="modal"><?=lang('close')?></a> 
                    <button type="submit" class="btn btn-primary">Add Activity</button>
        </div>
            </div>

    </div>
</div>

2 个答案:

答案 0 :(得分:0)

在您的代码中,每秒都会刷新整个页面。 代替“window.location.href”使用ajax调用并评估这样的脚本代码。

    $.ajax({
             url: "your url",
             success: function(data){
                    $(data).find("script").each(function() {
                    eval($(this).text()); 
                 }

            }
    });


   function alert_breaktime()
   {
      echo $this->load->view('break_alert');
   }

答案 1 :(得分:0)

毕竟,我得到了解决方案,它工作正常。所以,我在这里作为解决方案。

<script type="text/javascript">

      $(document).ready(function() {  

            var $timer = 0;

            function testbreak(){
                if ($timer === 0) {
                    $("#bb").click();
                    $timer = 1;
                }

            }

            setInterval(testbreak, 3000);
        });
    </script>


<div id='abc' style="display: none;">
<a href="<?=base_url()?>sidebar/alert_breaktime" id="bb" data-toggle="ajaxModal" class="btn btn-sm btn-primary pull-right"><i class="fa fa-plus"></i> Add Activity </a>
</div>