页脚脚本在codeigniter中不起作用 [英] footer scripts do not work in codeigniter

查看:81
本文介绍了页脚脚本在codeigniter中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在core文件夹中创建了MY_Controller.我在其中声明了public $footerScript;.这是MY_Controller的代码.

I have Created the MY_Controller in the core folder. In which I declared public $footerScript;. Here is the code of MY_Controller.

 <?php
    class MY_Controller extends CI_Controller
    {
       public $footerScript;
       public $data = array();
        public function __construct()
        {
            date_default_timezone_set( 'Asia/Karachi' );
            parent::__construct();
            $this->load->library(array('ion_auth','form_validation'));
            $this->data['C_FullName'] = 'CodeigNiter Shop';
            $this->data['C_ShortName'] = 'CI Shop';
            }
    }
    ?>

Home Controller扩展了MY_Controller.此Home Controller显示views文件夹中的add_items文件.

This Home Controller extends the MY_Controller. This Home Controller shows the add_items file in the views folder.

    <?php
    class Home extends MY_Controller
    {
      public function __construct()
      {
        parent::__construct();
        $this->load->library('image_lib');
      }
      public function items()
      {
          $this->show("admin/add_items");
      }
}
?>

这是add_items中的form,在其中将id赋予submit按钮.当我单击此按钮时,jquery event将被称为:

This is the form in the add_items in which I give id to the submit button. When I click this button a jquery event will be called :

<form id="form1" method="post" enctype="multipart/form-data">
   <div class="row">
      <div class="col-md-4">
         <div class="form-group">
            <label for="Header Text" class="control-label" > Title </i></label>
            <input type="text" placeholder="Title" id="title" name="title" class="form-control" tabindex="1">
         </div>
         <!-- /.form-group -->
      </div>
       <div class="row">
      <div class="col-md-2">
         <input type="submit" name="submit" id="Add" value="Add Items" class="btn btn-success">
      </div>
      <!-- iCheck -->
   </div>
   <!-- /.col (right) -->
</form>

,此代码写在add_items文件的末尾. script标记中的链接工作正常,但是当我单击按钮以显示alert时,它不起作用.

and this code is written at the end of the add_items file. The links in the script tag are working fine but when I click the button to show alert it does not work.

    <?php
        //This Section footerScripts Should Execute In Footer/End of the Page.
        $this->footerScript = sprintf('
        <script src="'.base_url().'assets/bower_components/jquery/dist/jquery.min.js"></script>
        <script src="'.base_url().'assets/plugins/datatables/jquery.dataTables.min.js"></script>        
         <script type="text/javascript">
                  $("#Add").on("click", function (e) {
                        e.preventDefault();
                         var title= $("#title").val();
                         alert(title); return false;
                       }
         </script>
        ');
        ?>

推荐答案

由于您正在使用jquery,因此请不要忘记添加如下准备好的文档:

Since you are using jquery, don't forget to add a document ready like:

$(function() {
  $("#Add").on("click", function (e) {
    e.preventDefault();
    var title= $("#title").val();
    alert(title); return false;
  }    
});

现在,它等待执行您的功能,直到DOM完全加载为止.参见文档

now it waits to execute your function until the DOM is loaded completely. see docs

这篇关于页脚脚本在codeigniter中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆