亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

頁面刷新后保留下拉列表的選定值

頁面刷新后保留下拉列表的選定值

PHP
夢里花落0921 2022-12-11 19:15:10
單擊頁面刷新后,我需要保留下拉列表的選定值。怎么了:從下拉列表中選擇一個項目后,頁面會自行重新加載,并且下拉列表的值會轉到起始位置,盡管該值顯示在 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>
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

一種更傳統的方法是使用 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>


查看完整回答
反對 回復 2022-12-11
  • 1 回答
  • 0 關注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號