简体   繁体   English

从确认对话框中选择“取消”时,如何停止重置表单输入字段?

[英]How do I stop form input fields from resetting when 'cancel' is selected from confirm dialog box?

I have a form with two buttons and an onclick js function:我有一个带有两个按钮和一个 onclick js function 的表单:

  <button name='button1' onclick="backFn()">Change Date Range</button>
  <button name='button2' type='submit' form='form1'>Save</button>

  <script>
   function backFn() {
     let back = confirm("Any unsaved changes will be lost");
       if (back == true) {
          location.href = '/selectdate.php';
       } else {
     }
   }
  </script>

Button2 submits the form as required - no issue there. Button2 按要求提交表单 - 没有问题。

Button1 triggers a function opening a confirm dialog box: 'OK' redirects as expected, but I want 'CANCEL' to close the dialog box but retain anything that's been written in the form. Button1 触发 function 打开确认对话框:“确定”按预期重定向,但我希望“取消”关闭对话框但保留表单中写入的任何内容。

At the moment the cancel option closes the dialog box, but clears the form.目前取消选项关闭对话框,但清除表单。 How can I retain the form selections/text?如何保留表格选择/文本? (If it's useful pressing 'back' afterwards repopulates the form.) (如果有用,之后按“后退”会重新填充表单。)

THANKS!谢谢!

Add type="button" to button1.将 type="button" 添加到 button1。

  <button name='button1' type="button" onclick="backFn()">Change Date Range</button>

Without this, the browser will assume the button is meant to submit the form.如果没有这个,浏览器将假定该按钮用于提交表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM