简体   繁体   English

使用javascript提交后如何保持选定的下拉列表值

[英]How keep the selected value for dropdown after submit using javascript

How do I keep the selected item after page refreshed ,i have a language in the multiple select, 页面刷新后如何保持所选项目,我在多种选择中有一种语言,

i am try now this with java script here is my code its not working ,can any one guide me how to make it selected 我现在尝试使用Java脚本执行此操作,这是我的代码无法正常工作,任何人都可以指导我如何选择它

my html code 我的html代码

<div class="control-group">
<label for="textfield" class="control-label">Language</label>
<div  class="controls">
    <select name="myLanguage" id="myLanguage" multiple="multiple">
        <option value="English,">English,</option>
        <option value="Arabic,">Arabic,</option>
        <option value="Hindi,">Hindi,</option>
        <option value="Malayalam,">Malayalam,</option>
        <option value="Danish,">Danish,</option>

    </select>
</div>
</div>

javascript JavaScript的

 document.getElementById('myLanguage').value = "<?php echo $_GET['language'];?>";

If you use jQuery use this: 如果您使用jQuery,请使用以下代码:

$(document).ready(function(){
    $('#myLanguage').val("<?php echo $_GET['language'];?>");
});

Javascript doesn't use a session variables, so adding the value to the session isnt possible. Javascript不使用会话变量,因此无法将值添加到会话中。 Still you can do two things: 您仍然可以做两件事:

Option 1: Add the value to a cookie with Jquery: 选项1:使用Jquery将值添加到Cookie:

Set: $.cookie("test", 1);

Read: var value = $.cookie("test");

See more: How do I set/unset cookie with jQuery? 查看更多: 如何使用jQuery设置/取消设置cookie?

Option 2: Post the value to the new page and request on that one: 选项2:将值发布到新页面并在该页面上进行请求:

Reading the value can be done with : 可以通过以下方式读取值:

Read: $_REQUEST["my_variable"];

Note: reading value can be done like: 注意:读取值可以像这样完成:

$("#myLanguage option:selected").text();

If you're using HTML5 as your doctype you can easily store values in the localStorage . 如果您使用HTML5作为doctype ,则可以轻松地将值存储在localStorage

Reference: http://www.w3schools.com/html/html5_webstorage.asp 参考: http : //www.w3schools.com/html/html5_webstorage.asp

To set it: 要设置它:

localStorage.setItem("variable_name", variable_value);

To get it: 为拿到它,为实现它:

localStorage.variable_name

To remove it: 删除它:

localStorage.removeItem("variable_name");

You can also use js cookies with jquery.cookie plugin, if you prefer. 如果愿意,还可以将js cookie和jquery.cookie插件一起使用。

As mentioned in the comments, when using the multiple attribute, 
the name attribute must be an array such as:

<div class="control-group">
    <label for="myLanguage[]" class="control-label">Language</label>
    <div  class="controls">
        <select name="myLanguage[]"  multiple="multiple"> 
            <option value="English,">English,</option>
            <option value="Arabic,">Arabic,</option>
            <option value="Hindi,">Hindi,</option>
            <option value="Malayalam,">Malayalam,</option>
            <option value="Danish,">Danish,</option>
        </select>
    </div>
</div>

javascript // Note: I'm not so sure on the following syntax

   var myLanguage[] = document.elements('myLanguage[]').value;
<?php
$languages = array(
  'en' => 'english',
  'vi' => 'vietnamese',
  'cn' => 'chinese'
);

if (isset($_GET['lang']) AND array_key_exists($_GET['lang'], $languages))
{
    include './lang/' . $languages[$_GET['lang']] . '.php';
}
else
{
    include './lang/english.php';

}
?>

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

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