简体   繁体   English

使用jQuery从下拉菜单中选择语言

[英]selecting language from drop down menu with jquery

http://jsfiddle.net/hr5aH/ http://jsfiddle.net/hr5aH/

I am jQuery beginner, I have just small problem here.. when I select language from the drop down menu the page refresh, but the drop down menu dose not show me which language I picked up..it's remain Choose Language - however the URL shows me which language I picked up, which is what I want, but not the drop down menu 我是jQuery初学者,我在这里只是个小问题。.当我从下拉菜单中选择语言时,页面会刷新,但是下拉菜单不会显示我选择了哪种语言。.仍然是选择语言-但是URL显示我选择的语言,这是我想要的,但不显示下拉菜单

for example if I choose from the drop down menu English 例如,如果我从下拉菜单中选择English

<script type="text/javascript">

$(document).ready(function(){

        $("form").change(function(){

            $("select[name='lang']").attr('selected','selected');
            $("form").submit();

        })

})
</script>

    </head>
    <body>

    <form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="get">

    <select name="lang">

        <option value="">Choose Language</option>
        <option value="English">English</option>
        <option value="Arabic">Arabic</option>
        <option value="French">French</option>

    </select>

I see the URL http://127.0.0.1/index.php?lang=english (Which what I want) 我看到了URL http://127.0.0.1/index.php?lang=english (我想要哪个)

but the drop down menu remain on choose language - I just want to the drop down menu shows me the language as well. 但下拉菜单仍保留选择语言-我只想下拉菜单也向我显示语言。

If you want to remeber the last language seleted then you need to use a cookie. 如果您想记住最后选择的语言,则需要使用Cookie。 Use the jquery cookie plugin. 使用jQuery cookie插件。

//checks if the cookie has been set

if($.cookie('remember_select') != null) {

    // set the option to selected that corresponds to what the cookie is set to

    $('.select_class option[value="' + $.cookie('remember_select') + '"]').attr('selected', 'selected');

}

// when a new option is selected this is triggered

$('.select_class').change(function() {

    // new cookie is set when the option is changed

    $.cookie('remember_select', $('.select_class option:selected').val(), { expires: 90, path: '/'});

});

Here is what your select would look like: 您的select如下所示:

<select class="select_class">
    <option value="1">Row 1</option>
    <option value="2">Row 2</option>
    <option value="3">Row 3</option>
</select>

Try with: 尝试:

$("select[name='lang']").change(function(){
    $("form").submit();
})

and to this you can add: 为此,您可以添加:

<option selected disable value="">Choose Language</option>

to prevent errors in you PHP 防止您的PHP中出现错误

EDIT: 编辑:

$_SESSION['language'] = $_GET['lang'] and in your jQuery $_SESSION['language'] = $_GET['lang']并在您的jQuery中

var selected_lang = '<?php echo $_SESSION["language"]; ?>';
$("select[name='lang']").val(selected_lang);

This needs a bit of php magic coupled with your HTML code: 这需要一点PHP魔术,再加上HTML代码:

PHP Code PHP代码

</head>
    <body>

    <form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="get">

    <select name="lang">

        <option value="">Choose Language</option>
        <?php foreach(array('English','Arabic','French') as $lang) ?>
        <option value="<?php echo $lang; ?>" <?php if ($_GET['lang']==$lang) echo "selected"; ?>><? echo $lang; ?></option>
       ?>
    </select>

Explanation: The php code checks for your url and selects the appropriate option from the select list. 说明: php代码检查您的URL,并从选择列表中选择适当的选项。

Tip: If you wish to have a cleaner code, place all your language values in an array and loop through it to generate your dropdown menu. 提示:如果您希望代码更简洁,请将所有语言值放在数组中并循环遍历以生成下拉菜单。

Your js script must be: 您的js脚本必须为:

$(document).ready(function(){

        $("form").change(function(){

            $("select[name='lang']").attr('selected','selected');
            $("form").submit();

        })
        var lang=getURLParameter('lang');
        if(lang!='null')
             $("select[name='lang']").prop('value',lang);

})
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

Link for details of getURLParameter() function: Get URL parameter with jQuery 链接以获取 getURLParameter()函数的详细信息: 使用jQuery获取URL参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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