无法在更新输入数值后执行表单操作。

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

Not able to perform form action after updating of input values

问题

我想要使用一个来自formspree的电子邮件服务,它使用以下输入格式:

<form
  action="https://formspree.io/f/*formendpoint* "
  method="POST"
>
  <label>
    你的邮箱:
    <input type="email" name="email">
  </label>
  <label>
    你的消息:
    <textarea name="message"></textarea>
  </label>
  <!-- 其他表单字段在这里添加 -->
  <button type="submit">发送</button>
</form>

由于这只允许两个输入,所以我正在使用一个表单来接受多个输入,并更新另一个表单的输入值,该表单将执行formspree功能。但是以下代码无法执行任务。

HTML:

<main>
    <div class="container">
        <p class="specialtext">联系我!</p>
        <form class="contactform" id="firstForm">
            <input type="text" id="name" placeholder="你的姓名" required>
            <input type="email" id="email" name="email" placeholder="电子邮件" required>
            <input type="text" id="phoneno" name="phone" placeholder="电话号码">
            <textarea name="message" id="message" placeholder="消息" cols="30" rows="10" required></textarea>
            <button type="submit">提交</button>
        </form>

        <form action="https://formspree.io/f/xoqzgybd" method="post" id="secondForm">
            <!-- <input type="email" name="name" > -->
            <input type="email" name="email">
            <textarea name="message" id="" cols="1" rows="1"></textarea>
        </form>

    </div>
</main>

脚本:

let firstForm = document.getElementById('firstForm');
let secondForm = document.getElementById('secondForm');
let emailInput = document.querySelector('#secondForm input[name="email"]');
let messageInput = document.querySelector('#secondForm textarea[name="message"]');

firstForm.addEventListener('submit', function(e){
    e.preventDefault();
    
    let emailtext = document.querySelector('#email').value;
    let messagetext = document.getElementById('name').value + "\n" +
        document.getElementById('phoneno').value + "\n" +
        document.getElementById('message').value;

    emailInput.value = emailtext;
    messageInput.value = messagetext;

    secondForm.submit();
});

我希望执行第二个表单的操作,并使用更新后的值。

英文:

I want to use an email service from formspree that uses input format :

&lt;form
  action=&quot;https://formspree.io/f/*formendpoint* &quot;
  method=&quot;POST&quot;
&gt;
  &lt;label&gt;
    Your email:
    &lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;
  &lt;/label&gt;
  &lt;label&gt;
    Your message:
    &lt;textarea name=&quot;message&quot;&gt;&lt;/textarea&gt;
  &lt;/label&gt;
  &lt;!-- your other form fields go here --&gt;
  &lt;button type=&quot;submit&quot;&gt;Send&lt;/button&gt;
&lt;/form&gt;

As this allows only two inputs. so I am using one form to take multiple inputs and update the input values of another form which will perform the formspree function. But the following code is unable to perform the task.

html:

&lt;main&gt;
        &lt;div class=&quot;container&quot;&gt;
            &lt;p class=&quot;specialtext&quot;&gt;Contact ME!&lt;/p&gt;
            &lt;form  class=&quot;contactform&quot; id=&quot;firstForm&quot;&gt;
                &lt;input type=&quot;text&quot; id=&quot;name&quot; placeholder=&quot;Your Name&quot; required&gt;
                &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;E-Mail&quot; required&gt;
                &lt;input type=&quot;text&quot; id=&quot;phoneno&quot; name=&quot;phone&quot; placeholder=&quot;Phone No.&quot;&gt;
                &lt;textarea name=&quot;message&quot; id=&quot;message&quot;  placeholder=&quot;Message&quot; cols=&quot;30&quot; rows=&quot;10&quot; required&gt;&lt;/textarea&gt;
                &lt;button type=&quot;submit&quot;&gt;SUBMIT&lt;/button&gt;
            &lt;/form&gt;

            &lt;form action=&quot;https://formspree.io/f/xoqzgybd&quot; method=&quot;post&quot; id=&quot;secondForm&quot;&gt;
                &lt;!-- &lt;input type=&quot;email&quot; name=&quot;name&quot; &gt; --&gt;
                &lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;
                &lt;textarea name=&quot;message&quot; id=&quot;&quot; cols=&quot;1&quot; rows=&quot;1&quot;&gt;&lt;/textarea&gt;
            &lt;/form&gt;

        &lt;/div&gt;
    &lt;/main&gt;

Script:

let firstForm = document.getElementById(&#39;firstForm&#39;);
let secondForm = document.getElementById(&#39;secondForm&#39;);
let emailInput = document.querySelector(&#39;#secondForm input[name=&quot;email&quot;]&#39;);
let messageInput = document.querySelector(&#39;#secondForm textarea[name=&quot;message&quot;]&#39;);

firstForm.addEventListener(&#39;submit&#39;, function(e){
    e.preventDefault();
    
    let emailtext=document.querySelector( &#39;#email&#39; ).value;
    let messagetext = document.getElementById(&#39;name&#39;).value + &quot;/n&quot; +
        document.getElementById(&#39;phoneno&#39;).value + &quot;/n&quot; +
        document.getElementById(&#39;message&#39;).value;

    emailInput.value = emailtext;
    messageInput.value = messagetext;

    secondForm.submit();
});

I want the action of second form performed with updated values.your text

答案1

得分: 0

确保您已正确拼写所有输入和输出。

这是一个更简单的脚本,更容易进行调试:

let firstForm = document.getElementById('firstForm');
let secondForm = document.getElementById('secondForm');
let emailInput = document.getElementById('email');
let emailOutput = document.getElementById('email1');
let messageOutput = document.getElementById('message1');

firstForm.addEventListener('submit', function(e) {
  e.preventDefault();
  emailOutput.value = document.getElementById('email').value;
  let messagetext = [];
  messagetext.push(document.getElementById('name').value);
  messagetext.push(document.getElementById('phoneno').value);
  messagetext.push(document.getElementById('message').value);
  messageOutput.value = messagetext.join("\n");
  secondForm.submit();
});
<main>
  <div class="container">
    <p class="specialtext">联系我!</p>
    <form class="contactform" id="firstForm">
      <input type="text" id="name" placeholder="您的姓名" required>
      <input type="email" id="email" name="email" placeholder="电子邮件" required>
      <input type="text" id="phoneno" name="phone" placeholder="电话号码">
      <textarea name="message" id="message" placeholder="留言" cols="30" rows="10" required></textarea>
      <button type="submit">提交</button>
    </form>
  </div>
</main>

<form action="https://formspree.io/f/xoqzgybd" method="post" id="secondForm" hidden>
  <input name="email" id="email1">
  <textarea name="message" id="message1"></textarea>
</form>

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

英文:

Make sure you have spell all the inputs and outputs correctly.

Here is a simpler script that is easier to debug

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

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

let firstForm = document.getElementById(&#39;firstForm&#39;);
let secondForm = document.getElementById(&#39;secondForm&#39;);
let emailInput = document.getElementById(&#39;email&#39;);
let emailOutput = document.getElementById(&#39;email1&#39;);
let messageOutput = document.getElementById(&#39;message1&#39;);

firstForm.addEventListener(&#39;submit&#39;, function(e) {
  e.preventDefault();
  emailOutput.value = document.getElementById(&#39;email&#39;).value;
  let messagetext = [];
  messagetext.push(document.getElementById(&#39;name&#39;).value);
  messagetext.push(document.getElementById(&#39;phoneno&#39;).value);
  messagetext.push(document.getElementById(&#39;message&#39;).value);
  messageOutput.value = messagetext.join(&quot;\n&quot;);
  secondForm.submit();
});

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

&lt;main&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;p class=&quot;specialtext&quot;&gt;Contact ME!&lt;/p&gt;
    &lt;form class=&quot;contactform&quot; id=&quot;firstForm&quot;&gt;
      &lt;input type=&quot;text&quot; id=&quot;name&quot; placeholder=&quot;Your Name&quot; required&gt;
      &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;E-Mail&quot; required&gt;
      &lt;input type=&quot;text&quot; id=&quot;phoneno&quot; name=&quot;phone&quot; placeholder=&quot;Phone No.&quot;&gt;
      &lt;textarea name=&quot;message&quot; id=&quot;message&quot; placeholder=&quot;Message&quot; cols=&quot;30&quot; rows=&quot;10&quot; required&gt;&lt;/textarea&gt;
      &lt;button type=&quot;submit&quot;&gt;SUBMIT&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/main&gt;

&lt;form action=&quot;https://formspree.io/f/xoqzgybd&quot; method=&quot;post&quot; id=&quot;secondForm&quot; hidden&gt;
  &lt;input name=&quot;email&quot; id=&quot;email1&quot;&gt;
  &lt;textarea name=&quot;message&quot; id=&quot;message1&quot;&gt;&lt;/textarea&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月17日 16:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702723.html
匿名

发表评论

匿名网友

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

确定