如何在codeigniter控制器中使用jquery?如果我使用这个方法,我必须从我的视图中删除我的其他jquery代码?

时间:2022-09-04 08:34:53

I have a jquery code in my view for search profile. search bar will appear on every page.I don't want to code this jquery in my every page ! so I want this jquery code in my controller my code in my view is here (This code working nicely in my view)

我在搜索配置文件的视图中有一个jquery代码。搜索栏将出现在每个页面上。我不想在每个页面中编写此jquery代码!所以我想在我的控制器中使用这个jquery代码我的视图中的代码就在这里(这段代码在我看来很好用)

<script type="text/javascript">  
$(document).ready(function(){
$("#display1").hide();
$(".search").keyup(function(){

var searchbox1 = $(this).val();
var datastring = 'searchword='+ searchbox1;    
if(searchbox1=='')
 {

 }
 else 
{
$.ajax({
type:"post",                       

url: "http://localhost:1337/PhpProject1/search_profile_controller",
data:datastring,
success:function(html){                             
$("#display1").html(html).show();

},
error:function(e){                         
alert("error="+e);
}

})                

}    

})         
})

1 个解决方案

#1


1  

Wrap this code in some .js file and upload it to your server folder to the path like: /var/www/html/application/javascripts/search.js save inside search.js:

将此代码包装在一些.js文件中,并将其上传到您的服务器文件夹,如下所示:/var/www/html/application/javascripts/search.js保存在search.js中:

I fixed some syntax and logic:

我修改了一些语法和逻辑:

$(document).ready(function(){
$("#display1").hide();
$(".search").keyup(function(){

    var searchbox1 = $(this).val();
    var datastring = 'searchword='+ searchbox1;

    if(searchbox1 != '')  {
        $.ajax({
            type:"post",
            url: "http://localhost:1337/PhpProject1/search_profile_controller",
            data:datastring,
            success:function(html){
                $("#display1").html(html).show();
            },
            error:function(e){
                alert("error="+e);
            }
        }); 
    }
    else
    {} //do nothing
  })
});

Then your view do this:

然后您的视图执行此操作:

<?php
$obj =& get_instance();
$load_searchjs = '<script src="'.APPPATH.'/javascripts/search.js" type=\"text/javascript\"></script>';
$obj->router->fetch_class() === 'name_of_home_controller' ? $load_searchjs : ''; //if we are located in some controller (path) then load search.js 
?>

#1


1  

Wrap this code in some .js file and upload it to your server folder to the path like: /var/www/html/application/javascripts/search.js save inside search.js:

将此代码包装在一些.js文件中,并将其上传到您的服务器文件夹,如下所示:/var/www/html/application/javascripts/search.js保存在search.js中:

I fixed some syntax and logic:

我修改了一些语法和逻辑:

$(document).ready(function(){
$("#display1").hide();
$(".search").keyup(function(){

    var searchbox1 = $(this).val();
    var datastring = 'searchword='+ searchbox1;

    if(searchbox1 != '')  {
        $.ajax({
            type:"post",
            url: "http://localhost:1337/PhpProject1/search_profile_controller",
            data:datastring,
            success:function(html){
                $("#display1").html(html).show();
            },
            error:function(e){
                alert("error="+e);
            }
        }); 
    }
    else
    {} //do nothing
  })
});

Then your view do this:

然后您的视图执行此操作:

<?php
$obj =& get_instance();
$load_searchjs = '<script src="'.APPPATH.'/javascripts/search.js" type=\"text/javascript\"></script>';
$obj->router->fetch_class() === 'name_of_home_controller' ? $load_searchjs : ''; //if we are located in some controller (path) then load search.js 
?>