首页 > 解决方案 > 在这种情况下,如何在 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>

标签: html

解决方案


如果您想在重置表单后保留值/选定的选项,您只需将点击事件添加到重置按钮并在重置之前存储选定的选项。

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>


推荐阅读