为什么我的函数在提交时没有被调用?

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

Why isn't my function being called on submit?

问题

尝试在以下表单提交时调用validateAndConfirm函数,但没有任何反应 - 请帮忙:(

HTML表单-

<div id='form' class='center text'>
    <form method='post' onsubmit='validateAndConfirm'>
        <label for="firstname">First Name:</label>
        <input type="text" name="firstname" id="firstname" placeholder='Your first name...' autocomplete='given-name' required>
        <br>
        <label for="lastname">Last Name:</label>
        <input type="text" id="lastname" name="lastname" placeholder='Your last name...' autocomplete='family-name' required>
        <br>
        <label for="email">Email Address:</label>
        <input type="email" id="email" name="email" placeholder="youremail@mail.co.nz..." autocomplete='off' required>
        <br>
        <label for="confemail">Confirm Email Address:</label>
        <input type="email" id="confemail" name="confemail" placeholder="youremail@mail.co.nz..." autocomplete='off' required>
        <br>
        <label for="phone">Contact Number:</label>
        <input type="tel" id="phone" name="phone" pattern='[0-9]{2-3}[0-9]{2-3}[0-9]{3-4}' placeholder='000 000 0000' autocomplete='tel-national' required>
        <br>
        <label for="address"> Address:</label>
        <input type="text" id="address" name="address" placeholder='Your address...' autocomplete='address' required>
        <br>
        <label for="gender"> Gender:</label>
        <select name="gender" id="gender" autocomplete='gender' required>
            <option value=''>Select...</option>
            <option value="gen">Gender Diverse</option>
            <option value="f">Female</option>
            <option value="m">Male</option>
            <option value="pnts">Prefer not to say</option>
        </select>
        <button class='center' type="submit" value='Signup'>Signup</button>
    </form>
</div>

JS函数-

function validateAndConfirm() {
    let firstname = document.getElementById('firstname').value;
    let lastname = document.getElementById('lastname').value;
    let email = document.getElementById('email').value;
    let confemail = document.getElementById('confemail').value;
    let phone = document.getElementById('phone').value;
    let address = document.getElementById('address').value;
    let gender = document.getElementById('gender').value;
    if (email == confemail) {
        alert('Your details have been submitted as below and your card will be sent out within the next 5 business days.\nFirst name: ' + firstname + '\nLast name: ' + lastname + '\nEmail: ' + email + '\nPhone ' + phone + '\nAddress: ' + address + '\nGender: ' + gender);
        return true;
    } else {
        alert('Emails do not match, please amend and try again.');
        return false;
    }
}

我期望它检查电子邮件是否匹配,如果为真,显示警告框,内容为"Your details have been submitted as below and your card will be sent out within the next 5 business days.
First name: firstname entered
Last name: lastname entered
Email: email entered
Phone: phone entered
Address: address entered
Gender: gender entered"。

如果为假,它将要求检查电子邮件并重试。

英文:

Trying to call validateAndConfirm function on submit of below form but nothing is happening - please help 为什么我的函数在提交时没有被调用?

HTML Form-

&lt;div id=&#39;form&#39; class=&#39;center text&#39;&gt;
&lt;form method=&#39;post&#39; onsumbmit=&#39;validateAndConfirm&#39;&gt;
&lt;label for=&quot;firstname&quot;&gt;First Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;firstname&quot; id=&quot;firstname&quot; placeholder=&#39;Your first name...&#39; autocomplete=&#39;given-name&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;lastname&quot;&gt;Last Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;lastname&quot; name=&quot;lastname&quot; placeholder=&#39;Your last name...&#39; autocomplete=&#39;family-name&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;email&quot;&gt;Email Address:&lt;/label&gt;
&lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;youremail@mail.co.nz...&quot; autocomplete=&#39;off&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;confemail&quot;&gt;Confirm Email Address:&lt;/label&gt;
&lt;input type=&quot;email&quot; id=&quot;confemail&quot; name=&quot;confemail&quot; placeholder=&quot;youremail@mail.co.nz...&quot; autocomplete=&#39;off&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;phone&quot;&gt;Contact Number:&lt;/label&gt;
&lt;input type=&quot;tel&quot; id=&quot;phone&quot; name=&quot;phone&quot; pattern=&#39;[0-9]{2-3}[0-9]{2-3}[0-9]{3-4}&#39; placeholder=&#39;000 000 0000&#39; autocomplete=&#39;tel-national&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;address&quot;&gt; Address:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;address&quot; name=&quot;address&quot; placeholder=&#39;Your address...&#39; autocomplete=&#39;address&#39; required&gt;
&lt;br&gt;
&lt;label for=&quot;gender&quot;&gt; Gender:&lt;/label&gt;
&lt;select name=&quot;gender&quot; id=&quot;gender&quot; autocomplete=&#39;gender&#39; required&gt;
&lt;option value=&#39;&#39;&gt;Select...&lt;/option&gt;
&lt;option value=&quot;gen&quot;&gt;Gender Diverse&lt;/option&gt;
&lt;option value=&quot;f&quot;&gt;Female&lt;/option&gt;
&lt;option value=&quot;m&quot;&gt;Male&lt;/option&gt;
&lt;option value=&quot;pnts&quot;&gt;Prefer not to say&lt;/option&gt;
&lt;/select&gt;
&lt;button class=&#39;center&#39; type=&quot;submit&quot; value=&#39;Signup&#39;&gt;Signup
&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;

JS function-

function validateAndConfirm() {
let firstname = document.getElementById(&#39;firstname&#39;).value;
let lastname = document.getElementById(&#39;lastname&#39;).value;
let email = document.getElementById(&#39;email&#39;).value;
let confemail = document.getElementById(&#39;confemail&#39;).value;
let phone = document.getElementById(&#39;phone&#39;).value;
let address = document.getElementById(&#39;address&#39;).value;
let gender = document.getElementById(&#39;gender&#39;).value;
if (email == confemail) {
alert(&#39;Your details have been submitted as below and your card will be sent out within the next 5 business days. /n First name: &#39; + firstname + &#39; /n Last name: &#39; + lastname + &#39; /n Email: &#39; + email + &#39; /n Phone &#39; + phone + &#39; /n Address: &#39; + address + &#39; /n Gender: &#39; + gender + &#39;&#39;);
return true;
} else {
alert(&#39;Emails do not match, please amend and try again.&#39;);
return false;
}
}

I am expecting this to check the emails match and if true display alert box saying "Your details have been submitted as below and your card will be sent out within the next 5 business days.
First name: firstname entered
Last name: lastname entered
Email: email entered
Phone: phone entered
Address: address entered
Gender: gender entered

If false it would ask to check emails and try again.

答案1

得分: 0

  1. 你的提交按钮位于表单之外,因此它不会尝试提交表单。

  2. 你的表单上的属性写成了 onsumbmit 而不是 onsubmit

将它移到闭合的表单标签内,并修复 onsubmit 属性:

<form method='post' onsubmit='return validateAndConfirm()'>

  <!-- ...输入项... -->

  <button type="submit" value="Signup">注册</button>  
</form>
英文:
  1. Your submit button is outside the form so it doesn't try to submit the form.

  2. Your attribute on the form says onsumbmit instead of onsubmit.

Move it inside the closing form tag and fix the onsubmit attributes:

&lt;form method=&#39;post&#39; onsubmit=&#39;return validateAndConfirm()&#39;&gt;

  &lt;!-- ...inputs... --&gt;

  &lt;button type=&quot;submit&quot; value=&quot;Signup&quot;&gt;Sign Up&lt;/button&gt;  
&lt;/form&gt;

答案2

得分: 0

3 个错误:

1 - 应该是 onsubmit 而不是 onsumbmit
2 - 函数应该像这样调用:validateAndConfirm()
3 - 输入按钮应该在表单内部

因此:

<form method='post' onsubmit='validateAndConfirm()'>
<input type='submit' />
</form>
英文:

3 errors:

<form method='post' onsumbmit='validateAndConfirm'>

1 - It is onsubmit not onsumbmit
2 - The function should be called like validateAndConfirm()
3 - The input button should be inside the form

Thus:

&lt;form method=&#39;post&#39; onsubmit=&#39;validateAndConfirm()&#39;&gt;
&lt;input type=&#39;submit&#39; /&gt;
&lt;/form&gt;

答案3

得分: -1

因为表单关闭标签的位置不正确并且使用了严格相等运算符(===)

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

<!-- language: lang-js -->
function validateAndConfirm() {
  let firstname = document.getElementById("firstname").value;
  let lastname = document.getElementById("lastname").value;
  let email = document.getElementById("email").value;
  let confemail = document.getElementById("confemail").value;
  let phone = document.getElementById("phone").value;
  let address = document.getElementById("address").value;
  let gender = document.getElementById("gender").value;

  if (email === confemail) {
    alert("Your details have been submitted as below and your card will be sent out within the next 5 business days.\n\nFirst name: " + firstname + "\nLast name: " + lastname + "\nEmail: " + email + "\nPhone: " + phone + "\nAddress: " + address + "\nGender: " + gender);
    return true;
  } else {
    alert("Emails do not match, please amend and try again.");
    return false;
  }
}

<!-- language: lang-html -->
<div id="form" class="center text">
  <form method="post" onsubmit="return validateAndConfirm()">
    <label for="firstname">First Name:</label>
    <input type="text" name="firstname" id="firstname" placeholder="Your first name..." autocomplete="given-name" required>
    <br>
    <label for="lastname">Last Name:</label>
    <input type="text" id="lastname" name="lastname" placeholder="Your last name..." autocomplete="family-name" required>
    <br>
    <label for="email">Email Address:</label>
    <input type="email" id="email" name="email" placeholder="youremail@mail.co.nz..." autocomplete="off" required>
    <br>
    <label for="confemail">Confirm Email Address:</label>
    <input type="email" id="confemail" name="confemail" placeholder="youremail@mail.co.nz..." autocomplete="off" required>
    <br>
    <label for="phone">Contact Number:</label>
    <input type="tel" id="phone" name="phone" pattern="[0-9]{2-3}[0-9]{2-3}[0-9]{3-4}" placeholder="000 000 0000" autocomplete="tel-national" required>
    <br>
    <label for="address">Address:</label>
    <input type="text" id="address" name="address" placeholder="Your address..." autocomplete="address" required>
    <br>
    <label for="gender"> Gender:</label>
    <select name="gender" id="gender" autocomplete="gender" required>
      <option value="">Select...</option>
      <option value="gen">Gender Diverse</option>
      <option value="f">Female</option>
      <option value="m">Male</option>
      <option value="pnts">Prefer not to say</option>
    </select>
    <br>
    <button type="submit">Signup</button>
  </form>
</div>

<!-- end snippet -->
英文:

Because the form closing tag is not in the correct position and use strick equality operator(===)

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

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

  function validateAndConfirm() {
let firstname = document.getElementById(&quot;firstname&quot;).value;
let lastname = document.getElementById(&quot;lastname&quot;).value;
let email = document.getElementById(&quot;email&quot;).value;
let confemail = document.getElementById(&quot;confemail&quot;).value;
let phone = document.getElementById(&quot;phone&quot;).value;
let address = document.getElementById(&quot;address&quot;).value;
let gender = document.getElementById(&quot;gender&quot;).value;
if (email === confemail) {
alert(&quot;Your details have been submitted as below and your card will be sent out within the next 5 business days.\n\nFirst name: &quot; + firstname + &quot;\nLast name: &quot; + lastname + &quot;\nEmail: &quot; + email + &quot;\nPhone: &quot; + phone + &quot;\nAddress: &quot; + address + &quot;\nGender: &quot; + gender);
return true;
} else {
alert(&quot;Emails do not match, please amend and try again.&quot;);
return false;
}
}

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

&lt;div id=&quot;form&quot; class=&quot;center text&quot;&gt;
&lt;form method=&quot;post&quot; onsubmit=&quot;return validateAndConfirm()&quot;&gt;
&lt;label for=&quot;firstname&quot;&gt;First Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;firstname&quot; id=&quot;firstname&quot; placeholder=&quot;Your first name...&quot; autocomplete=&quot;given-name&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;lastname&quot;&gt;Last Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;lastname&quot; name=&quot;lastname&quot; placeholder=&quot;Your last name...&quot; autocomplete=&quot;family-name&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;email&quot;&gt;Email Address:&lt;/label&gt;
&lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;youremail@mail.co.nz...&quot; autocomplete=&quot;off&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;confemail&quot;&gt;Confirm Email Address:&lt;/label&gt;
&lt;input type=&quot;email&quot; id=&quot;confemail&quot; name=&quot;confemail&quot; placeholder=&quot;youremail@mail.co.nz...&quot; autocomplete=&quot;off&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;phone&quot;&gt;Contact Number:&lt;/label&gt;
&lt;input type=&quot;tel&quot; id=&quot;phone&quot; name=&quot;phone&quot; pattern=&quot;[0-9]{2-3}[0-9]{2-3}[0-9]{3-4}&quot; placeholder=&quot;000 000 0000&quot; autocomplete=&quot;tel-national&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;address&quot;&gt; Address:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;address&quot; name=&quot;address&quot; placeholder=&quot;Your address...&quot; autocomplete=&quot;address&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;gender&quot;&gt; Gender:&lt;/label&gt;
&lt;select name=&quot;gender&quot; id=&quot;gender&quot; autocomplete=&quot;gender&quot; required&gt;
&lt;option value=&quot;&quot;&gt;Select...&lt;/option&gt;
&lt;option value=&quot;gen&quot;&gt;Gender Diverse&lt;/option&gt;
&lt;option value=&quot;f&quot;&gt;Female&lt;/option&gt;
&lt;option value=&quot;m&quot;&gt;Male&lt;/option&gt;
&lt;option value=&quot;pnts&quot;&gt;Prefer not to say&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;button type=&quot;submit&quot;&gt;Signup&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案4

得分: -1

  1. onsubmit 应该作为 &lt;form&gt; 标签的属性提供,而不是作为按钮的 onclick 属性。

  2. 提交按钮应该位于 &lt;form&gt; 内部。

PS:我已经添加了 event.preventDefault() 以避免在提交按钮点击时出现不必要的页面刷新。

<div id='form' class='center text' onsubmit="validateAndConfirm(event)">
    <form method='post'>
        <!-- 所有您的控件 -->
        <button class='center' type="submit" value="Signup">Signup</button>
    </form>
</div>
function validateAndConfirm(event) {
  event.preventDefault();
  console.log('Hello world')
  // 函数
}
英文:

Two things

  1. onsubmit should be provided as an attribute to the &lt;form&gt; tag instead of onclick in button

  2. Submit button should be inside the &lt;form&gt;

PS: I have added an event.preventDefault() inorder to avoid unnecessary page refresh on submit button click.

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

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

function validateAndConfirm(event) {
event.preventDefault();
console.log(&#39;Hello world&#39;)
// Function
}

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

&lt;div id=&#39;form&#39; class=&#39;center text&#39; onsubmit=&quot;validateAndConfirm(event)&quot;&gt;
&lt;form method=&#39;post&#39;&gt;
&lt;!-- All your controls --&gt;
&lt;button class=&#39;center&#39; type=&quot;submit&quot; value=&quot;Signup&quot;&gt;Signup
&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定