获取下拉菜单的选定文本而不提交按钮

时间:2014-02-24 13:31:28

标签: javascript jquery html drop-down-menu

我有这个下拉菜单:

if(isset($_POST['ss'])){$_SESSION['ss'] = $_POST['ss'] ;}else{$_SESSION['ss'] = 'English';}


<select name = "selectlang" class="selectlang" onchange="location = this.options[this.selectedIndex].value;">
        <option  value='<?php $website_link ?>?lang=english' <?php if($_SESSION['ss'] == "English"){echo ' selected="selected"' ;} ?>>English</option>
        <option  value='<?php $website_link ?>?lang=swedish' <?php if($_SESSION['ss'] == "Swedish"){echo ' selected="selected"' ;} ?>>Swedish</option>
        <option  value='<?php $website_link ?>?lang=russian' <?php if($_SESSION['ss'] == "Russian"){echo ' selected="selected"' ;} ?>>Russian</option>
</select>
<input type="hidden" name="ss" id="lang_hidden" value="" />

这实际上当我选择其提交的一种语言并在网址链接中添加?lang=.....时。它运作良好,网址会改变下拉菜单总是英文的方式。

如何将其更改为所选语言?

我已经使用了它,但它没有用。

$(".selectlang").change(function(){
var inpvalue = $('.selectlang option:selected').text();
    $("#lang_hidden").val(inpvalue).val();
   //$("#lang_hidden").closest('form').trigger('submit'); //(i will explain this)
  });

我在上面解释了这一行,当我取消注释并添加表单标签

  <form method="POST" action="">
 the above html code
  </form>

下拉菜单会保留所选语言,但不要更改lang=.....和未翻译网页的链接。

如何管理它们以保持选定的下拉菜单文本并更改链接?

提前致谢。

 //wanna see my function language so let me know.

我的函数语言和我在哪里制作会话变量可能错误就在这里。

   function setlanguage(){
    //$_SESSION['selectlang'] = '';
    //if(!isset($_SESSION['selectlang'])){$_SESSION['selectlang'] = 'English' ;}
    //if(!isset($_GET['lang'])){$_GET['lang'] = 'english' ;}
    if(isset($_GET['lang'])){
        switch ($_GET['lang']){
            case "english" :
                    $_SESSION['selectlang'] = 'English';
                  include("lang/english.php");
                  break;
            case "swedish" :
                    $_SESSION['selectlang'] = 'Swedish';
                   include("lang/swedish.php");
                   break;
            case "russian":
                    $_SESSION['selectlang'] = 'Russian';
                   include("lang/russian.php");
                 break;

                }
        }
    else if(!isset($_GET['lang']) and isset($_SESSION['selectlang'])){
        switch ($_SESSION['selectlang']){
            case "English" :
                    $_SESSION['selectlang'] = 'English';
                  include("lang/english.php");
                  break;
            case "Swedish" :
                    $_SESSION['selectlang'] = 'Swedish';
                   include("lang/swedish.php");
                   break;
            case "Russian":
                    $_SESSION['selectlang'] = 'Russian';
                   include("lang/russian.php");
                 break;
            default :
                    include("lang/english.php");
        }
    }
    else{include("lang/english.php"); $_SESSION['selectlang'] = 'English';}

   }

4 个答案:

答案 0 :(得分:1)

您使用不同的会话变量名称

在php中

$_SESSION['selectlang'] = 'Russian';
HTML中的

$_SESSION['ss'] == "Russian"

答案 1 :(得分:0)

当您重定向页面时,如果您确定,将值保存到会话变量,则问题可能是字母大小写。

value='<?php $website_link ?>?lang=english'

变化

<?php if($_SESSION['ss'] == "English"){echo ' selected="selected"' ;} ?>

<?php if($_SESSION['ss'] == "english"){echo ' selected="selected"' ;} ?>

答案 2 :(得分:0)

尝试以下选项。

 <option  value='<?php $website_link ?>?lang=english' <?php if($_SESSION['ss'] == "english"){echo "selected" ;} ?>>English</option>

or 

 <option  value='<?php $website_link ?>?lang=english' <?php if($_REQUEST['lang'] == "english"){echo "selected" ;} ?>>English</option>

答案 3 :(得分:0)

尝试此更改您的网址而不加载网页。

pageurl = 'yourpage.php?lang=inpvalue;
if(pageurl!=window.location){
    window.history.pushState({path:pageurl},'',pageurl);
}