更改由下拉触发的jquery函数

时间:2013-10-24 15:35:10

标签: jquery html

我一直试图解决这个问题几个小时,但没有成功。我也搜索了现有的问题,但没有找到正确的答案: - /

我目前在我的网站上有一个下拉菜单,它会触发一个功能来更改会话表上的活动语言。我试图做的是摆脱丑陋的下拉,并用fancybox取代它来改变语言。一切正常,除了触发更改功能,我需要你的帮助来更改jquery代码,以便与下拉功能相同,但现在由文本链接触发。这是旧的下拉:

下拉:

<form id="language-selection-form" method="post" action="<?php echo base_url();?>/index.php/home/save_lang_change">
            <?php $lang = $this->session->userdata('language'); ?>
            <select id="language-selection" name="language-selection">
                <option value="spanish" <?php echo (!empty($lang) && $lang == 'spanish') ? 'selected="selected"' : ''; ?>>Espanol</option>
                <option value="german" <?php echo (!empty($lang) && $lang == 'german') ? 'selected="selected"' : ''; ?>>Deutsch</option>
                <option value="english" <?php echo (!empty($lang) && $lang == 'english') ? 'selected="selected"' : ''; ?>>English</option>
            </select>
        </form>

jQuery的:

<script type="text/javascript">
var http_host_url = "<?php echo base_url(); ?>";
jQuery(document).ready(function(){
    jQuery('#language-selection').change(function(){
        /*var lang = $(this).val();
        $.ajax({
            type:'post',
            url: http_host_url + "index.php/home/save_lang_change",
            data: {
                lang: lang
            },
            success: function(html){
                console.log(html);
            }
        }) */

        jQuery('#language-selection-form').submit();

    })
})</script>

现在,新脚本将是:

<a class="fancybox" href="#lang_change"><img src="<?php echo base_url() ?>app/css/images/flags/es.png" /> Español</a>
    <div id="lang_change" style="width:200px;display: none;">
                <h3>Choose your language:</h3><br/>
                <p align="center"><img src="<?php echo base_url() ?>app/css/images/flags/es.png" />&nbsp;Español</p>
                <p align="center"><img src="<?php echo base_url() ?>app/css/images/flags/gb.png" />&nbsp;English</p>
                <p align="center"><img src="<?php echo base_url() ?>app/css/images/flags/de.png" />&nbsp;Deutsch</p>
    </div>

但当然,它不会触发jquery函数。

这是home.php控制器中的函数:

function save_lang_change(){

    //echo $_POST['language-selection']  . " hjkh ff";
    $lang = $_POST['language-selection'];
    $this->load->library('session');
    $this->session->set_userdata("language", $lang);
    //echo $this->session->userdata("language");
    redirect(base_url());

 }

有关如何解决此问题的任何想法?提前致谢; - )

3 个答案:

答案 0 :(得分:0)

更改

jQuery('#language-selection').change(function(){

jQuery("#lang_change p").click(function(){

答案 1 :(得分:0)

做这样的事情:

    <script type="text/javascript">
    var http_host_url = "<?php echo base_url(); ?>";
function changeLang(lang){
            $.ajax({
                type:'post',
                url: http_host_url + "index.php/home/save_lang_change",
                data: {
                    lang: lang
                },
                success: function(html){
                    console.log(html);
                }
            }) ;

            jQuery('#language-selection-form').submit();

        }
</script>
<a class="fancybox" href="#lang_change"><img src="<?php echo base_url() ?>app/css/images/flags/es.png" /> Español</a>
    <div id="lang_change" style="width:200px;display: none;">
                <h3>Choose your language:</h3><br/>
                <p align="center" onclick="changeLang('es');"><img src="<?php echo base_url() ?>app/css/images/flags/es.png" />&nbsp;Español</p>
                <p align="center" changeLang('en');><img src="<?php echo base_url() ?>app/css/images/flags/gb.png" />&nbsp;English</p>
                <p align="center" changeLang('de');><img src="<?php echo base_url() ?>app/css/images/flags/de.png" />&nbsp;Deutsch</p>
    </div>

答案 2 :(得分:0)

jquery / javascript是不必要的。我修改了home.php控制器,以便为每种语言提供一个函数:

     function save_lang_change_es(){

    $lang = spanish;
    $this->load->library('session');
    $this->session->set_userdata("language", $lang);
    //echo $this->session->userdata("language");
    redirect(base_url());

 }

 function save_lang_change_en(){

    $lang = english;
    $this->load->library('session');
    $this->session->set_userdata("language", $lang);
    //echo $this->session->userdata("language");
    redirect(base_url());

 }

 function save_lang_change_de(){

    $lang = german;
    $this->load->library('session');
    $this->session->set_userdata("language", $lang);
    //echo $this->session->userdata("language");
    redirect(base_url());

 }

并更改链接如下:

<div id="lang_change" style="width:200px;display: none;">
                <h3>Choose your language:</h3><br/>
                <p align="center"><a href="<?php echo base_url();?>/index.php/home/save_lang_change_es"><img src="<?php echo base_url() ?>app/css/images/flags/es.png" />&nbsp;Español</a></p>
                <p align="center"><a href="<?php echo base_url();?>/index.php/home/save_lang_change_en"><img src="<?php echo base_url() ?>app/css/images/flags/gb.png" />&nbsp;English</a></p>
                <p align="center"><a href="<?php echo base_url();?>/index.php/home/save_lang_change_de"><img src="<?php echo base_url() ?>app/css/images/flags/de.png" />&nbsp;Deutsch</a></p>
    </div>

现在,每个链接都调用自己的函数,语言切换工作:)