繁体   English   中英

页面刷新后保留下拉列表的选定值

[英]Retain selected value of the dropdown after page refreshes

单击页面刷新后,我需要保留下拉列表的选定值。

怎么了:

After I select an item from the dropdown the page reloads by itself and the value of the dropdown go to starting position though the value displays on the end of the url.

这是我的代码:

<script>
function refreshPage(passValue){
    window.location="index.php?cat_page=admin_post&sw_cat=sw_catn="+passValue;
}
</script>


<div class="form-group col-md-4">
    <label for="inputState">Your health proplem</label>
    <select name="illness_id_cat" class="form-control" onchange="refreshPage(this.value);" >
<?php 
while(mysqli_stmt_fetch($stmt)){
    echo "<option value='{$illness_id_cat}'>{$illness_name_cat}</option>";
}
?>
    </select>
</div>

此选项也不起作用:

<script>
var selectedItem = sessionStorage.getItem("SelectedItem");  
$('#dropdown').val(selectedItem);

$('#dropdown').change(function() { 
    var dropVal = $(this).val();
    sessionStorage.setItem("SelectedItem", dropVal);
});
</script>

<div class="form-group col-md-4">
    <label for="inputState">Your health proplem</label>
    <select name="illness_id_cat" class="form-control" id="dropdown" >
<?php 
while(mysqli_stmt_fetch($stmt)){
    echo "<option value='{$illness_id_cat}'>{$illness_name_cat}</option>";
}
?>
    </select>
</div>

这个我不知道怎么实现,什么是“itemname”:

<script>
var text = localStorage.getItem("itemname");
if(text !== null) {
    $("select option").filter(function() {
        return this.text == text;
    }).attr('selected', true);
}
</script>

更传统的方法是使用 PHP,并通过 window.location 命令获取您传入的查询字符串值:

<script>
function refreshPage(passValue){
    window.location="index.php?cat_page=admin_post&sw_cat="+passValue;
}
</script>


<div class="form-group col-md-4">
    <label for="inputState">Your health problem</label>
    <select name="illness_id_cat" class="form-control" onchange="refreshPage(this.value);" >
<?php 
while(mysqli_stmt_fetch($stmt)){
    echo "<option";
    //get the category value from the querystring and check it against the current category value in the loop. If it matches, pre-set the option as selected
    if (isset($_GET["sw_cat"])) {
      if ($_GET["sw_cat"] == $illness_id_cat) echo " selected";
    }
    echo " value='{$illness_id_cat}'>{$illness_name_cat}</option>";

}
?>
    </select>
</div>

暂无
暂无

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

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