addeventlistener on button does not retrieve the latest values of text inputs

huangapple go评论57阅读模式
英文:

addeventlistener on button does not retrieve the latest values of text inputs

问题

以下是您提供的内容的中文翻译:

我有一个包含4个字段集的表单,每个字段集都有文本输入框和一个按钮,该按钮将仅发送与相应字段集的数据的POST请求。

提交操作按预期工作,但它始终发送文本输入框的初始值,即使值已更改(请参阅代码片段)。

我还尝试在添加事件监听器之前为给定的字段集元素创建变量,但console.log仍然显示初始值而不是新值...

编辑:下面的代码示例显示.elements的日志,但目标是获取输入的名称和值,以便在POST请求中发送它们。

<html>
  <head></head>
  <body>
    <form id="test-form">
      <fieldset for="test-form">
        <input type="text" value="初始值" name="input-test-1" />
        <button type="submit" name="btn-submit">提交 1</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-2" />
        <button type="submit" name="btn-submit">提交 2</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-3" />
        <button type="submit" name="btn-submit">提交 3</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-4" />
        <button type="submit" name="btn-submit">提交 4</button>
      </fieldset>
      <button type="submit" name="btn-main-submit">其他提交</button>
    </form>
  </body>
</html>

如果您需要更多帮助,请告诉我。

英文:

I have a form that contains 4 fieldsets, each fieldset has text inputs and a button that will send a POST request with the data of the corresponding fieldset only.

The submit action works as expected but it always send the initial values of text inputs, even if the values have changed (see the code snippet).

I also tried to create a variable for a given fieldset element before adding the eventlistener but the console.log was still displaying the initial values and not the new values...

EDIT : the code below shows the log of .elements as an example, but the goal was to get names & values of inputs to send them in a POST request.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const ready = (e) =&gt; {
  let form = document.querySelector(&#39;#test-form&#39;);

  form.querySelectorAll(&#39;button[name=&quot;btn-submit&quot;]&#39;).forEach((el) =&gt; {
    el.addEventListener(&#39;click&#39;, (e) =&gt; {
      e.preventDefault();
      
      console.log([...el.closest(&#39;fieldset&#39;).elements]);
    });
  });
};

document.addEventListener(&#39;DOMContentLoaded&#39;, ready);

<!-- language: lang-html -->

&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;form id=&quot;test-form&quot;&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;initial value&quot; name=&quot;input-test-1&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 1&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-2&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 2&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-3&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 3&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-4&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 4&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;button type=&quot;submit&quot; name=&quot;btn-main-submit&quot;&gt;Other submit&lt;/button&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

在HTML元素上运行console.log仅显示元素的原始HTML表示。如果您想要其属性的当前值(而不是其原始属性),您需要修改您的代码以记录您想要的属性:

在这里,我正在使用 Array.from 的一种重载,它接受一个映射函数。

const ready = (e) => {
  let form = document.querySelector('#test-form');

  form.querySelectorAll('button[name="btn-submit"]').forEach((el) => {
    el.addEventListener('click', (e) => {
      e.preventDefault();
      
      console.log(Array.from(el.closest('fieldset').elements, (el) => el.value));
    });
  });
};

document.addEventListener('DOMContentLoaded', ready);
<html>
  <head></head>
  <body>
    <form id="test-form">
      <fieldset for="test-form">
        <input type="text" value="initial value" name="input-test-1" />
        <button type="submit" name="btn-submit">Submit 1</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-2" />
        <button type="submit" name="btn-submit">Submit 2</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-3" />
        <button type="submit" name="btn-submit">Submit 3</button>
      </fieldset>
      <fieldset for="test-form">
        <input type="text" value="" name="input-test-4" />
        <button type="submit" name="btn-submit">Submit 4</button>
      </fieldset>
      <button type="submit" name="btn-main-submit">Other submit</button>
    </form>
  </body>
</html>

这是您提供的代码的翻译部分。

英文:

Running console.log on HTML Elements merely shows the original HTML representation of the element. If you want the current values of its properties (not its original attributes), you need to amend your code to log the property(ies) you want:

Here I'm using the overload of Array.from that takes a mapping function.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

const ready = (e) =&gt; {
  let form = document.querySelector(&#39;#test-form&#39;);

  form.querySelectorAll(&#39;button[name=&quot;btn-submit&quot;]&#39;).forEach((el) =&gt; {
    el.addEventListener(&#39;click&#39;, (e) =&gt; {
      e.preventDefault();
      
      console.log(Array.from(el.closest(&#39;fieldset&#39;).elements, (el) =&gt; el.value));
    });
  });
};

document.addEventListener(&#39;DOMContentLoaded&#39;, ready);

<!-- language: lang-html -->

&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;form id=&quot;test-form&quot;&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;initial value&quot; name=&quot;input-test-1&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 1&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-2&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 2&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-3&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 3&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;fieldset for=&quot;test-form&quot;&gt;
        &lt;input type=&quot;text&quot; value=&quot;&quot; name=&quot;input-test-4&quot; /&gt;
        &lt;button type=&quot;submit&quot; name=&quot;btn-submit&quot;&gt;Submit 4&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;button type=&quot;submit&quot; name=&quot;btn-main-submit&quot;&gt;Other submit&lt;/button&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 21:12:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定