selectbox 我选择的选项回到顶部

selectbox the option I chose goes back to the top

When the page is redirected, the language I chose in the box does not appear. At first, whatever is available is chosen.

here is what I want to do: change option data

<select id='lang' class='btn btn-xs btn-primary icons-select2'>
<?php
$query_lang=mysqli_query($con, "SELECT * FROM tb_lang");
while($lang = mysqli_fetch_assoc($query_lang)){
if ($lang['durum']==1)}
?>

<option value = "<?php echo $lang['lang_fix']; ?>"  data-min="_<?php echo $lang['lang_fix']; ?>" 
data-icon="<?php echo $lang['lang_flag']; ?>">
<?php
echo strtoupper($lang['lang_fix']);
?>
</option>

<?php }} ?>
</select>

<script>
$(document).ready(function () {   
  $('body').on('change','#lang', function() {
   var lang_id= $(this).val();
   window.location.replace("index.php?lang="+lang_id);
 });
});
</script>

首先检索

通过 GET 发送的 lang
$lang_id = $_GET['lang'];

然后根据您正在循环的数据检查它

<option value = "<?php echo $lang['lang_fix']; ?>"  data-min="_<?php echo $lang['lang_fix']; ?>" data-icon="<?php echo $lang['lang_flag']; ?>" <?php if ($lang_id == $lang['lang_fix']) echo "selected"; ?>>

使您的代码如下所示,因此您要选择的选项将具有必要的 selected 属性。

$lang_id = $_GET['lang'];
<select id='lang' class='btn btn-xs btn-primary icons-select2'>
<?php
$query_lang=mysqli_query($con, "SELECT * FROM tb_lang");
while($lang = mysqli_fetch_assoc($query_lang)){
if ($lang['durum']==1)}
?>

<option value = "<?php echo $lang['lang_fix']; ?>"  data-min="_<?php echo $lang['lang_fix']; ?>" data-icon="<?php echo $lang['lang_flag']; ?>" <?php if ($lang_id == $lang['lang_fix']) echo "selected"; ?>>
<?php
echo strtoupper($lang['lang_fix']);
?>
</option>

<?php }} ?>
</select>

<script>
$(document).ready(function () {   
  $('body').on('change','#lang', function() {
   var lang_id= $(this).val();
   window.location.replace("index.php?lang="+lang_id);
 });
});
</script>