Ajax 形式:新的重置状态

Ajax form: new reset state

本文关键字:状态 形式 Ajax      更新时间:2023-09-26

我有一个通过jquery ajax提交的表单。如何设置通过 ajax 提交表单时的新重置状态?

例如,表单中有一个输入,在页面加载时默认值为"foo"。用户将值更改为"bar",点击保存(jquery通过ajax将表单数据发送到服务器)。现在,如果用户进行其他更改并且用户点击表单重置按钮,我希望它重置为"bar"而不是"foo"。

想要捕获整个表单的"重置"状态,而不仅仅是特定输入。关于如何实现这一目标的任何见解?

试试这个:

<form>
    <input type="text" name="demoInput" value="foo" />
    <button type="reset">Reset</button>
    <button type="submit">Submit</button>
</form>
<script>
    $('body').on('click', 'button[type=submit]', function () {
      // Store the current value as a data attribute
      $('input').each(function() {
        $(this).data('value',$(this).val());
      });
    });
    $('body').on('click', 'button[type=reset]', function () {
      // Restore the store value
      $('input').each(function() {
        $(this).val($(this).data('value'));
      });
    });
</script>

我现在不能尝试,但设置输入的默认值属性可能是一种选择。

旧问题,但在成功提交后将defaultValue设置为value将使重置按钮适用于新状态。

简单的 JQuery 示例:

$("form input").each((ix, t) => { t.defaultValue = t.value; });