html - 在这种情况下,如何在 html/php 中将先前的选择存储在下拉菜单中
问题描述
我已经下拉更改我已经设置了确认,单击确定提交表单这部分工作正常但是单击取消选择仍然在下拉列表中更改如何防止这种情况发生。
当我单击取消下拉菜单时,如何存储我之前的选择并设置它?
<td style="width:auto;text-align:center">
<select class="form-control" name="Ostatus" onchange="if(confirm('Are you sure you want change the status?')){this.form.submit()}">
<option value="Uploaded" <?php if ($dispatchstatus == "Uploaded") echo "selected='selected'";?> >Uploaded</option>
<option value="Processing" <?php if ($dispatchstatus == "Processing") echo "selected='selected'";?> >Processing</option>
<option value="Dispatched" <?php if ($dispatchstatus == "Dispatched") echo "selected='selected'";?> >Dispatched</option>
</select>
</td>
解决方案
如果您想在重置表单后保留值/选定的选项,您只需将点击事件添加到重置按钮并在重置之前存储选定的选项。
document.getElementById("resetbtn").addEventListener("click", function(event){
event.preventDefault()
var select = document.getElementById("selection");
var option = select.options[select.selectedIndex].text;
document.getElementById("mainform").reset();
select.value = option;
});
<form id="mainform">
<select id="selection">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="test">
<button type="reset" value="Reset" id="resetbtn">Reset</button>
</form>
推荐阅读
- c# - 带有 FtpWebRequest 上传的 C# FTP 失败并显示“534 策略需要 ssl”
- javascript - 如何使用 Javascript 在输入标签上设置默认日期时间戳?
- node.js - NodeJS api没有返回任何响应
- php - PHP在嵌套循环中超出内存分配
- python - 这样的正则表达式哪里错了?
- reactjs - 使用默认值反应路由参数
- php - 如何更改按钮的标题(kartik FileInput 小部件)
- azure - 使用 ADF 复制活动映射使用变量编写动态列名的最简单方法是什么?
- unit-testing - redux saga 测试取消(任务):参数 [object Object] 不是有效的 Task 对象
- python - 在数据框中分组后如何保持该行空间?