0

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

我目前在我的网站上有一个下拉菜单,它触发了更改会话表上活动语言的功能。我试图做的是摆脱丑陋的下拉菜单并用花式框替换它来更改语言。除了触发更改功能外,一切正常,我需要您的帮助来更改 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());

 }

关于如何解决这个问题的任何想法?提前致谢 ;-)

4

3 回答 3

0

改变

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

jQuery("#lang_change p").click(function(){
于 2013-10-24T15:43:00.540 回答
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>

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

于 2013-10-24T16:29:45.307 回答
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>
于 2013-10-24T15:46:13.877 回答