第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從表單 HTML/JS 捕獲用戶輸入

從表單 HTML/JS 捕獲用戶輸入

滄海一幻覺 2023-09-07 16:41:04
我目前正在嘗試從表單捕獲輸入數(shù)據(jù)并將該數(shù)據(jù)存儲(chǔ)到對象數(shù)組中,然后該對象數(shù)組可以匯總控制臺(tái)中捕獲的數(shù)據(jù)。最重要的是,我嘗試按照我的作業(yè)說明運(yùn)行該函數(shù),使用傳統(tǒng)的 DOM 事件處理程序來存儲(chǔ)該數(shù)據(jù)。JS 的最終目標(biāo)是使用 var t 通過 ID 'contactSubButton' 獲取提交按鈕的元素,并運(yùn)行函數(shù) contactInfo onsubmit...但是,無論我嘗試什么,我都會(huì)在控制臺(tái)的“t.addEventListener”行中收到此消息......Uncaught TypeError: Cannot read property 'addEventListener' of nullvar t = document.getElementById("contactSubButton");t.addEventListener('click', contactInfo());function contactInfo(fName, lName, email, country, subject) {  fName = document.getElementById('fname').value;  lName = document.getElementById('lname').value;  email = document.getElementById('email').value;  country = document.getElementById('country').value;  subject = document.getElementById('subject').value;  for (i = 0;; i++) {    var subInfo = [fName, lName, email, country, subject]  }  console.log('Your submission details are:' < br > 'First Name: ' + fName)}<div id="form">  <form>    <label for="fname">First Name</label>    <input type="text" id="fname" name="firstname" placeholder="Your first name...">    <label for="lname">Last Name</label>    <input type="text" id="lname" name="lastname" placeholder="Your last name....">    <label for="email">Email Address</label>    <input type="email" id="email" name="email" placeholder="Please enter your email...">    <label for="country">Country</label>    <select id="country" name="country">      <option value="usa">United States</option>      <option value="canada">Canada</option>      <option value="mexico">Mexico</option>    </select>    <label for="subject">Subject</label>    <textarea id="subject" name="subject" placeholder="How can we help?"></textarea>    <input type="submit" value="Submit" id="contactSubButton">    <input type="reset" value="Reset Form">  </form></div>  
查看完整描述

2 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊

  • 你有一個(gè)永遠(yuǎn)不會(huì)結(jié)束的循環(huán)for (i = 0;; i++) {

  • 你在這里傳遞不必要的變量contactInfo(fName, lName, email, country, subject) {

  • 您錯(cuò)誤地分配了事件偵聽器。你需要t.addEventListener('click', contactInfo);沒有()

  • 你的引述是錯(cuò)誤的console.log('Your submission details are:' < br > 'First Name: ' + fName)

我想你想要這個(gè)

  • 我使用提交事件

  • 如果用戶按取消,我會(huì)取消提交事件

function contactInfo(event) { // passing the submit event

  let fName = document.getElementById('fname').value,

    lName = document.getElementById('lname').value,

    email = document.getElementById('email').value,

    country = document.getElementById('country').value,

    subject = document.getElementById('subject').value;


  const text = `Your submission details are:

    First Name: ${fName}

    Last Name:  ${lName}

    Email:      ${email}

    Country:    ${country}

    Subject:    ${subject}

    Send this now?`

  

  if (!confirm(text)) event.preventDefault(); // stop submission by cancelling the event passed

}


window.addEventListener("load", function() { // when page loads

  document.getElementById("contactForm").addEventListener('submit', contactInfo);


});

<div id="form">

  <form id="contactForm">

    <label for="fname">First Name</label>

    <input type="text" id="fname" name="firstname" placeholder="Your first name...">


    <label for="lname">Last Name</label>

    <input type="text" id="lname" name="lastname" placeholder="Your last name....">


    <label for="email">Email Address</label>

    <input type="email" id="email" name="email" placeholder="Please enter your email...">


    <label for="country">Country</label>

    <select id="country" name="country">

      <option value="usa">United States</option>

      <option value="canada">Canada</option>

      <option value="mexico">Mexico</option>

    </select>


    <label for="subject">Subject</label>

    <textarea id="subject" name="subject" placeholder="How can we help?"></textarea>


    <input type="submit" value="Submit" id="contactSubButton">

    <input type="reset" value="Reset Form">


  </form>


查看完整回答
反對 回復(fù) 2023-09-07
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

嘗試這樣的事情:


網(wǎng)頁:


<form method="post" id="myForm">

   <input type="text" name="firstname" placeholder="Your first name...">

   <input type="submit" value="Submit" />

</form>

JS:


document.getElementById('myForm').addEventListener ("submit", function (evt) {

   evt.preventDefault();

   var formData = new FormData(document.getElementById('myForm'))

   console.log('Your submission details are:' < br > 'First Name: ' + formData.get('firstname'))

});


查看完整回答
反對 回復(fù) 2023-09-07
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號