gpt4 book ai didi

javascript - 重新加载页面后 Ajax 值保持不变

转载 作者:行者123 更新时间:2023-12-03 00:31:27 24 4
gpt4 key购买 nike

我正在使用 2 个下拉列表。一份列表来自 Ajax 代码。我的问题是,如何在重新加载表单时确保该值保留在第二个下拉列表中。我正在使用触发器更改事件来获取模型下拉列表的值。我的代码如下

---我

  • 触发更改我从堆栈溢出中使用的 Jquery。--我还有一个表名模型,车辆的值来自make表。

--

<?php
include "conn.php";
$exe=$con->query("select * from make");

?>

<!DOCTYPE html>
<html>
<head>
<title>Ajax</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$('#make').trigger("change");
//console.log(make);
function getModel()
{
var mid= document.getElementById("make").value;
//console.log(mid);
$.ajax({
type:"POST",
url :"model.php",
data:"makeid="+mid,
success:function(ans)
{
document.getElementById("model").innerHTML=ans;


}
});
}


</script>
</head>
<body>
<form method="post">
<table align="center">
<tr>
<td>Make</td>
<td>
<select name="make" id="make" onchange="return getModel()">
<?php
while ($row=$exe->fetch_object())
{
?>
<option value="<?php echo $row->make_id?>">
<?php if(isset($_POST['make']) && $_POST['make']==$row->make_id) echo "selected";?>
<?php echo $row->make_name?></option>
<?php
}
?>
</select>

</td>
</tr>
<tr>
<td>Model</td>
<td><select name="model" id="model">
<option value="">.....</option>
</select></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

最佳答案

此代码会将您的下拉值存储到存储中

let dropdownModel= document.getElementById("model").value
localStorage.setItem("myValue", dropdownModel);

此代码将从下拉列表中加载您的存储值并将其放回其中

document.getElementById("model").value = localStorage.getItem("myValue");

关于javascript - 重新加载页面后 Ajax 值保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53848827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com