简体   繁体   中英

How to pass jQuery scripts with codeIgneter

My goal is to pass a jQuery script through a model with CodeIgneter.

This is the view where I linked the jquery.js file

  <footer>
   <p>&copy; Company 2014</p>
  </footer>      
  <script src="<?php echo base_url('assets/js/jquery.js');?>">  </script>

   </div> <!-- /container -->

This is the model jquery

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jquery extends CI_Model { 

public function test(){

return 

'<script>
$(document).ready(function() {
$("#submitForm").click(function(){
alert("test");
});

});

</script>';
}
}

In the controller I passed the script as following:

$this->load->model('jquery');
$data['testjs'] = $this->jquery->test();
$this->load->view('login/content',$data);

In the content view I echo the script:

...echo $testjs...

Unfortunately the script does not work as expected.

you need to include jquery library. Add this in your header view

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

and then this code in somewhere in view or header view

jQuery(document).ready(function() {
  jQuery("#submitForm").click(function(){
    alert("this is working file ");
  });
});

you can use Dom crawler for that if you want to send html and jquery data pass through controller

https://github.com/dimabdc/PHP-Fast-Simple-HTML-DOM-Parser

or else you can set in header view file as above answer given

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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