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

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


怎么了:


从下拉列表中选择一个项目后,页面会自行重新加载,并且下拉列表的值会转到起始位置,尽管该值显示在 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>


梦里花落0921
浏览 125回答 1
1回答

慕工程0101907

一种更传统的方法是使用 PHP,并通过 window.location 命令获取您传递的查询字符串值:<script>function refreshPage(passValue){&nbsp; &nbsp; window.location="index.php?cat_page=admin_post&sw_cat="+passValue;}</script><div class="form-group col-md-4">&nbsp; &nbsp; <label for="inputState">Your health problem</label>&nbsp; &nbsp; <select name="illness_id_cat" class="form-control" onchange="refreshPage(this.value);" ><?php&nbsp;while(mysqli_stmt_fetch($stmt)){&nbsp; &nbsp; echo "<option";&nbsp; &nbsp; //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&nbsp; &nbsp; if (isset($_GET["sw_cat"])) {&nbsp; &nbsp; &nbsp; if ($_GET["sw_cat"] == $illness_id_cat) echo " selected";&nbsp; &nbsp; }&nbsp; &nbsp; echo " value='{$illness_id_cat}'>{$illness_name_cat}</option>";}?>&nbsp; &nbsp; </select></div>
打开App,查看更多内容
随时随地看视频慕课网APP