繁体   English   中英

第一次加载页面时出现“ TypeError:表单未定义”

I am getting a “TypeError: form is undefined” when I first load my page

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我正在开发一种表单,可以将新的招聘数据传输到我们的JSON招聘中。 我刚开始,但是Node.js还是一个新手,我正在测试。 您能否让我知道如何防止出现此错误。

错误:TypeError:表单未定义http:// localhost:63342 / VAP_FORM / index.html:32:11

浏览器:firefox

 /** * Retrieves input data from a form and returns it as a JSON object. * @param {HTMLFormControlsCollection} elements the form elements * @return {Object} form data as an object literal */ const formToJSON = elements => [].reduce.call(elements, (data, element) => { data[element.name] = element.value; return data; }, {}); /** * A handler function to prevent default submission and run our custom script. * @param {Event} event the submit event triggered by the user * @return {void} */ const handleFormSubmit = event => { event.preventDefault(); const data = formToJSON(form.elements); const dataContainer = document.getElementsByClassName('results__display')[0]; dataContainer.textContent = JSON.stringify(data, null, " "); console.log(dataContainer); }; /* We find the form element using its class name, then attach the * `handleFormSubmit()` function to the * `submit` event. */ const form = document.getElementsByClassName('contact-form')[0]; form.addEventListener('submit', handleFormSubmit); 
 .valid{ border:1px solid blue; } .invalid{ border:1px solid red; } .column { float: left; padding: 10px; height: 300px; /* Should be removed. Only for demonstration */ } .left{ width: 10%; } .right { width: 30%; } .middle { width: 60%; min-width: 300px; height: 100%; } .row:after { content: ""; display: table; clear: both; } #vapheading { color: black; font-family: Avenir; } #form{ color: white; background-color: navy; padding: 16px; border-radius: 25px; } @media only screen and (max-width: 1026px) { #left,#right { display: none; } } input { border: 4px solid whitesmoke; border-radius: 25px; background-color: whitesmoke; } select { border: 4px solid whitesmoke; border-radius: 25px; background-color: whitesmoke; } body{ background-color: lightgrey; color: white; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="vap_css.css"/> <script src="vap_script.js"></script> <script> function validateForm() { var x = document.forms["myForm"]["ssn1"].value; var y = document.forms["myForm"]["ssn2"].value; if (x !== y ){ alert("Social Security number does not match"); return false; } } </script> </head> <body> <div class="row"> <div class="column left" id="left"></div> <div class="column middle" id="middle"> <h2 id = "vapheading">Please fill out the Following Form</h2> <div id = "form"> <form class="contact-form__label" name="myForm" onsubmit="return validateForm()"> <table id = "table"> <tr> <td>First Name:* <br/><input type="text" name="firstname" id="fname" required/></td> <td>Middle Name: <br/><input type="text" name="middlename" id="mname" /></td> <td>Last Name:* <br/><input type="text" name="lastname" id="lname" required/></td> </tr> <tr> <td>Suffix: <br/><input type="text" name="suffix" id="suffix" /></td> </tr> </table> <table> <tr> <td>SSN:* <br/><input class="required-input" id="ssn1" maxlength="9" type="password" name="ssn1" required/></td> <td>Verify SSN:* <br/><input class="required-input" id="ssn2" maxlength="9" type="password" name="ssn2" required/></td> <td>Date of Birth:* <br/><input type="date" name="birthday" id="bday" required/></td> </tr> </table> <table> <tr> <td>Place of Birth - City:* <br/><input type="text" name="city" id="city" required/></td> <td>Place of Birth - State: <br/><input type="text" name="state" id="state" ></td> <td>Place of Birth - Country:* <br/><select name="country" id="country" required> <option value="">Choose Here</option> <option value="Afghanistan">Afghanistan</option><option value="Albania">Albania</option><option value="Algeria">Algeria</option><option value="Andorra">Andorra</option> <option value="Angola">Angola</option><option value="Antigua & Deps">Antigua & Deps</option><option value="Argentina">Argentina</option> <option value="Armenia">Armenia</option><option value="Australia">Australia</option><option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option><option value="Bahamas">Bahamas</option><option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option><option value="Barbados">Barbados</option><option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option><option value="Belize">Belize</option><option value="Benin">Benin</option> <option value="Bhutan">Bhutan</option><option value="Bolivia">Bolivia</option><option value="Bosnia Herzegovina">Bosnia Herzegovina</option> <option value="Botswana">Botswana</option><option value="Brazil">Brazil</option><option value="Brunei">Brunei</option> <option value="Bulgaria">Bulgaria</option><option value="Burkina">Burkina</option><option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option><option value="Cameroon">Cameroon</option><option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option><option value="Central African Rep">Central African Rep</option><option value="Chad">Chad</option> <option value="Chile">Chile</option><option value="China">China</option><option value="Colombia">Colombia</option> <option value="Comoros">Comoros</option><option value="Congo">Congo</option><option value="Congo {Democratic Rep}">Congo {Democratic Rep}</option> <option value="Costa Rica">Costa Rica</option><option value="Croatia">Croatia</option><option value="Cuba">Cuba</option> <option value="Cyprus">Cyprus</option><option value="Czech Republic">Czech Republic</option><option value="Denmark">Denmark</option> <option value="Djibouti">Djibouti</option><option value="Dominica">Dominica</option><option value="Dominican Republic">Dominican Republic</option> <option value="East Timor">East Timor</option><option value="Ecuador">Ecuador</option><option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option><option value="Equatorial Guinea">Equatorial Guinea</option><option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option><option value="Ethiopia">Ethiopia</option><option value="Fiji">Fiji</option> <option value="Finland">Finland</option><option value="France">France</option><option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option><option value="Georgia">Georgia</option><option value="Germany">Germany</option> <option value="Ghana">Ghana</option><option value="Greece">Greece</option><option value="Grenada">Grenada</option> <option value="Guatemala">Guatemala</option><option value="Guinea">Guinea</option><option value="Guinea-Bissau">Guinea-Bissau</option> <option value="Guyana">Guyana</option><option value="Haiti">Haiti</option><option value="Honduras">Honduras</option> <option value="Hungary">Hungary</option><option value="Iceland">Iceland</option><option value="India">India</option> <option value="Indonesia">Indonesia</option><option value="Iran">Iran</option><option value="Iraq">Iraq</option> <option value="Ireland {Republic}">Ireland {Republic}</option><option value="Israel">Israel</option><option value="Italy">Italy</option> <option value="Ivory Coast">Ivory Coast</option><option value="Jamaica">Jamaica</option><option value="Japan">Japan</option> <option value="Jordan">Jordan</option><option value="Kazakhstan">Kazakhstan</option><option value="Kenya">Kenya</option> <option value="Kiribati">Kiribati</option><option value="Korea North">Korea North</option><option value="Korea South">Korea South</option> <option value="Kosovo">Kosovo</option><option value="Kuwait">Kuwait</option><option value="Kyrgyzstan">Kyrgyzstan</option> <option value="Laos">Laos</option><option value="Latvia">Latvia</option><option value="Lebanon">Lebanon</option> <option value="Lesotho">Lesotho</option><option value="Liberia">Liberia</option><option value="Libya">Libya</option> <option value="Liechtenstein">Liechtenstein</option><option value="Lithuania">Lithuania</option><option value="Luxembourg">Luxembourg</option> <option value="Macedonia">Macedonia</option><option value="Madagascar">Madagascar</option><option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option><option value="Maldives">Maldives</option><option value="Mali">Mali</option> <option value="Malta">Malta</option><option value="Marshall Islands">Marshall Islands</option><option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option><option value="Mexico">Mexico</option><option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option><option value="Monaco">Monaco</option><option value="Mongolia">Mongolia</option> <option value="Montenegro">Montenegro</option><option value="Morocco">Morocco</option><option value="Mozambique">Mozambique</option> <option value="Myanmar, {Burma}">Myanmar, {Burma}</option><option value="Namibia">Namibia</option><option value="Nauru">Nauru</option> <option value="Nepal">Nepal</option><option value="Netherlands">Netherlands</option><option value="New Zealand">New Zealand</option> <option value="Nicaragua">Nicaragua</option><option value="Niger">Niger</option><option value="Nigeria">Nigeria</option> <option value="Norway">Norway</option><option value="Oman">Oman</option><option value="Pakistan">Pakistan</option> <option value="Palau">Palau</option><option value="Panama">Panama</option><option value="Papua New Guinea">Papua New Guinea</option> <option value="Paraguay">Paraguay</option><option value="Peru">Peru</option><option value="Philippines">Philippines</option> <option value="Poland">Poland</option><option value="Portugal">Portugal</option><option value="Qatar">Qatar</option> <option value="Romania">Romania</option><option value="Russian Federation">Russian Federation</option><option value="Rwanda">Rwanda</option> <option value="St Kitts & Nevis">St Kitts & Nevis</option><option value="St Lucia">St Lucia</option><option value="Saint Vincent & the Grenadines">Saint Vincent & the Grenadines</option> <option value="Samoa">Samoa</option><option value="San Marino">San Marino</option><option value="Sao Tome & Principe">Sao Tome & Principe</option> <option value="Saudi Arabia">Saudi Arabia</option><option value="Senegal">Senegal</option><option value="Serbia">Serbia</option> <option value="Seychelles">Seychelles</option><option value="Sierra Leone">Sierra Leone</option><option value="Singapore">Singapore</option> <option value="Slovakia">Slovakia</option><option value="Slovenia">Slovenia</option><option value="Solomon Islands">Solomon Islands</option> <option value="Somalia">Somalia</option><option value="South Africa">South Africa</option><option value="South Sudan">South Sudan</option> <option value="Spain">Spain</option><option value="Sri Lanka">Sri Lanka</option><option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option><option value="Swaziland">Swaziland</option><option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option><option value="Syria">Syria</option><option value="Taiwan">Taiwan</option> <option value="Tajikistan">Tajikistan</option><option value="Tanzania">Tanzania</option><option value="Thailand">Thailand</option> <option value="Togo">Togo</option><option value="Tonga">Tonga</option><option value="Trinidad & Tobago">Trinidad & Tobago</option> <option value="Tunisia">Tunisia</option><option value="Turkey">Turkey</option><option value="Turkmenistan">Turkmenistan</option> <option value="Tuvalu">Tuvalu</option><option value="Uganda">Uganda</option><option value="Ukraine">Ukraine</option> <option value="United Arab Emirates">United Arab Emirates</option><option value="United Kingdom">United Kingdom</option><option value="United States">United States</option> <option value="Uruguay">Uruguay</option><option value="Uzbekistan">Uzbekistan</option><option value="Vanuatu">Vanuatu</option> <option value="Vatican City">Vatican City</option><option value="Venezuela">Venezuela</option><option value="Vietnam">Vietnam</option> <option value="Yemen">Yemen</option><option value="Zambia">Zambia</option><option value="Zimbabwe">Zimbabwe</option> </select></td> </tr> <tr> <td>Citizenship:* <br/><select name="citizenship" id="citixen" default required> <option value="">Choose Here</option> <option value="USCitizen">US Citizen</option> <option value="greencard">Green Card</option> <option value="H1B">H1B</option> <option value="EAD">EAD</option> <option value="Other">Other</option> </select></td> </tr> </table> <br/><br/><button type="submit">Submit</button> </form> </div> </div> <div class="column right" id="right"></div> </div> </body> </html> 

2 个回复

您的班级名称选择器不起作用,请使用完整的班级名称contact-form__label ,也可以使用getElementsByName代替。 但是,也要在加载DOM之前导入脚本,因此您将需要在正文底部移动脚本导入。

如果出于某种原因您不想移动脚本,则可以将表单选择包装在事件中,该事件将在页面竞争准备就绪时执行,如下所示:

document.addEventListener("DOMContentLoaded", function(event) { 
  let form = document.getElementsByClassName('contact-form__label')[0];
  form.addEventListener('submit', handleFormSubmit);
});

 /** * Retrieves input data from a form and returns it as a JSON object. * @param {HTMLFormControlsCollection} elements the form elements * @return {Object} form data as an object literal */ const formToJSON = elements => [].reduce.call(elements, (data, element) => { data[element.name] = element.value; return data; }, {}); /** * A handler function to prevent default submission and run our custom script. * @param {Event} event the submit event triggered by the user * @return {void} */ const handleFormSubmit = event => { event.preventDefault(); const data = formToJSON(form.elements); const dataContainer = document.getElementsByClassName('results__display')[0]; dataContainer.textContent = JSON.stringify(data, null, " "); console.log(dataContainer); }; /* We find the form element using its class name, then attach the * `handleFormSubmit()` function to the * `submit` event. */ let form = document.getElementsByClassName('contact-form__label')[0]; form.addEventListener('submit', handleFormSubmit); 
 .valid{ border:1px solid blue; } .invalid{ border:1px solid red; } .column { float: left; padding: 10px; height: 300px; /* Should be removed. Only for demonstration */ } .left{ width: 10%; } .right { width: 30%; } .middle { width: 60%; min-width: 300px; height: 100%; } .row:after { content: ""; display: table; clear: both; } #vapheading { color: black; font-family: Avenir; } #form{ color: white; background-color: navy; padding: 16px; border-radius: 25px; } @media only screen and (max-width: 1026px) { #left,#right { display: none; } } input { border: 4px solid whitesmoke; border-radius: 25px; background-color: whitesmoke; } select { border: 4px solid whitesmoke; border-radius: 25px; background-color: whitesmoke; } body{ background-color: lightgrey; color: white; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="vap_css.css"/> <script> function validateForm() { var x = document.forms["myForm"]["ssn1"].value; var y = document.forms["myForm"]["ssn2"].value; if (x !== y ){ alert("Social Security number does not match"); return false; } } </script> </head> <body> <div class="row"> <div class="column left" id="left"></div> <div class="column middle" id="middle"> <h2 id = "vapheading">Please fill out the Following Form</h2> <div id = "form"> <form class="contact-form__label" name="myForm" onsubmit="return validateForm()"> <table id = "table"> <tr> <td>First Name:* <br/><input type="text" name="firstname" id="fname" required/></td> <td>Middle Name: <br/><input type="text" name="middlename" id="mname" /></td> <td>Last Name:* <br/><input type="text" name="lastname" id="lname" required/></td> </tr> <tr> <td>Suffix: <br/><input type="text" name="suffix" id="suffix" /></td> </tr> </table> <table> <tr> <td>SSN:* <br/><input class="required-input" id="ssn1" maxlength="9" type="password" name="ssn1" required/></td> <td>Verify SSN:* <br/><input class="required-input" id="ssn2" maxlength="9" type="password" name="ssn2" required/></td> <td>Date of Birth:* <br/><input type="date" name="birthday" id="bday" required/></td> </tr> </table> <table> <tr> <td>Place of Birth - City:* <br/><input type="text" name="city" id="city" required/></td> <td>Place of Birth - State: <br/><input type="text" name="state" id="state" ></td> <td>Place of Birth - Country:* <br/><select name="country" id="country" required> <option value="">Choose Here</option> <option value="Afghanistan">Afghanistan</option><option value="Albania">Albania</option><option value="Algeria">Algeria</option><option value="Andorra">Andorra</option> <option value="Angola">Angola</option><option value="Antigua & Deps">Antigua & Deps</option><option value="Argentina">Argentina</option> <option value="Armenia">Armenia</option><option value="Australia">Australia</option><option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option><option value="Bahamas">Bahamas</option><option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option><option value="Barbados">Barbados</option><option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option><option value="Belize">Belize</option><option value="Benin">Benin</option> <option value="Bhutan">Bhutan</option><option value="Bolivia">Bolivia</option><option value="Bosnia Herzegovina">Bosnia Herzegovina</option> <option value="Botswana">Botswana</option><option value="Brazil">Brazil</option><option value="Brunei">Brunei</option> <option value="Bulgaria">Bulgaria</option><option value="Burkina">Burkina</option><option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option><option value="Cameroon">Cameroon</option><option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option><option value="Central African Rep">Central African Rep</option><option value="Chad">Chad</option> <option value="Chile">Chile</option><option value="China">China</option><option value="Colombia">Colombia</option> <option value="Comoros">Comoros</option><option value="Congo">Congo</option><option value="Congo {Democratic Rep}">Congo {Democratic Rep}</option> <option value="Costa Rica">Costa Rica</option><option value="Croatia">Croatia</option><option value="Cuba">Cuba</option> <option value="Cyprus">Cyprus</option><option value="Czech Republic">Czech Republic</option><option value="Denmark">Denmark</option> <option value="Djibouti">Djibouti</option><option value="Dominica">Dominica</option><option value="Dominican Republic">Dominican Republic</option> <option value="East Timor">East Timor</option><option value="Ecuador">Ecuador</option><option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option><option value="Equatorial Guinea">Equatorial Guinea</option><option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option><option value="Ethiopia">Ethiopia</option><option value="Fiji">Fiji</option> <option value="Finland">Finland</option><option value="France">France</option><option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option><option value="Georgia">Georgia</option><option value="Germany">Germany</option> <option value="Ghana">Ghana</option><option value="Greece">Greece</option><option value="Grenada">Grenada</option> <option value="Guatemala">Guatemala</option><option value="Guinea">Guinea</option><option value="Guinea-Bissau">Guinea-Bissau</option> <option value="Guyana">Guyana</option><option value="Haiti">Haiti</option><option value="Honduras">Honduras</option> <option value="Hungary">Hungary</option><option value="Iceland">Iceland</option><option value="India">India</option> <option value="Indonesia">Indonesia</option><option value="Iran">Iran</option><option value="Iraq">Iraq</option> <option value="Ireland {Republic}">Ireland {Republic}</option><option value="Israel">Israel</option><option value="Italy">Italy</option> <option value="Ivory Coast">Ivory Coast</option><option value="Jamaica">Jamaica</option><option value="Japan">Japan</option> <option value="Jordan">Jordan</option><option value="Kazakhstan">Kazakhstan</option><option value="Kenya">Kenya</option> <option value="Kiribati">Kiribati</option><option value="Korea North">Korea North</option><option value="Korea South">Korea South</option> <option value="Kosovo">Kosovo</option><option value="Kuwait">Kuwait</option><option value="Kyrgyzstan">Kyrgyzstan</option> <option value="Laos">Laos</option><option value="Latvia">Latvia</option><option value="Lebanon">Lebanon</option> <option value="Lesotho">Lesotho</option><option value="Liberia">Liberia</option><option value="Libya">Libya</option> <option value="Liechtenstein">Liechtenstein</option><option value="Lithuania">Lithuania</option><option value="Luxembourg">Luxembourg</option> <option value="Macedonia">Macedonia</option><option value="Madagascar">Madagascar</option><option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option><option value="Maldives">Maldives</option><option value="Mali">Mali</option> <option value="Malta">Malta</option><option value="Marshall Islands">Marshall Islands</option><option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option><option value="Mexico">Mexico</option><option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option><option value="Monaco">Monaco</option><option value="Mongolia">Mongolia</option> <option value="Montenegro">Montenegro</option><option value="Morocco">Morocco</option><option value="Mozambique">Mozambique</option> <option value="Myanmar, {Burma}">Myanmar, {Burma}</option><option value="Namibia">Namibia</option><option value="Nauru">Nauru</option> <option value="Nepal">Nepal</option><option value="Netherlands">Netherlands</option><option value="New Zealand">New Zealand</option> <option value="Nicaragua">Nicaragua</option><option value="Niger">Niger</option><option value="Nigeria">Nigeria</option> <option value="Norway">Norway</option><option value="Oman">Oman</option><option value="Pakistan">Pakistan</option> <option value="Palau">Palau</option><option value="Panama">Panama</option><option value="Papua New Guinea">Papua New Guinea</option> <option value="Paraguay">Paraguay</option><option value="Peru">Peru</option><option value="Philippines">Philippines</option> <option value="Poland">Poland</option><option value="Portugal">Portugal</option><option value="Qatar">Qatar</option> <option value="Romania">Romania</option><option value="Russian Federation">Russian Federation</option><option value="Rwanda">Rwanda</option> <option value="St Kitts & Nevis">St Kitts & Nevis</option><option value="St Lucia">St Lucia</option><option value="Saint Vincent & the Grenadines">Saint Vincent & the Grenadines</option> <option value="Samoa">Samoa</option><option value="San Marino">San Marino</option><option value="Sao Tome & Principe">Sao Tome & Principe</option> <option value="Saudi Arabia">Saudi Arabia</option><option value="Senegal">Senegal</option><option value="Serbia">Serbia</option> <option value="Seychelles">Seychelles</option><option value="Sierra Leone">Sierra Leone</option><option value="Singapore">Singapore</option> <option value="Slovakia">Slovakia</option><option value="Slovenia">Slovenia</option><option value="Solomon Islands">Solomon Islands</option> <option value="Somalia">Somalia</option><option value="South Africa">South Africa</option><option value="South Sudan">South Sudan</option> <option value="Spain">Spain</option><option value="Sri Lanka">Sri Lanka</option><option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option><option value="Swaziland">Swaziland</option><option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option><option value="Syria">Syria</option><option value="Taiwan">Taiwan</option> <option value="Tajikistan">Tajikistan</option><option value="Tanzania">Tanzania</option><option value="Thailand">Thailand</option> <option value="Togo">Togo</option><option value="Tonga">Tonga</option><option value="Trinidad & Tobago">Trinidad & Tobago</option> <option value="Tunisia">Tunisia</option><option value="Turkey">Turkey</option><option value="Turkmenistan">Turkmenistan</option> <option value="Tuvalu">Tuvalu</option><option value="Uganda">Uganda</option><option value="Ukraine">Ukraine</option> <option value="United Arab Emirates">United Arab Emirates</option><option value="United Kingdom">United Kingdom</option><option value="United States">United States</option> <option value="Uruguay">Uruguay</option><option value="Uzbekistan">Uzbekistan</option><option value="Vanuatu">Vanuatu</option> <option value="Vatican City">Vatican City</option><option value="Venezuela">Venezuela</option><option value="Vietnam">Vietnam</option> <option value="Yemen">Yemen</option><option value="Zambia">Zambia</option><option value="Zimbabwe">Zimbabwe</option> </select></td> </tr> <tr> <td>Citizenship:* <br/><select name="citizenship" id="citixen" default required> <option value="">Choose Here</option> <option value="USCitizen">US Citizen</option> <option value="greencard">Green Card</option> <option value="H1B">H1B</option> <option value="EAD">EAD</option> <option value="Other">Other</option> </select></td> </tr> </table> <br/><br/><button type="submit">Submit</button> </form> </div> </div> <div class="column right" id="right"></div> </div> <script src="vap_script.js"></script> </body> </html> 

您应该在JavaScript代码的开头添加以下行:

var form = document.forms[0];

现在,您可以访问form变量。

2 Vue.prototype 在页面第一次加载时未定义

所以,我试图在我的main.js文件中使用 axios 发出请求。 如图所示,我正在使用 vue-router 在加载每个组件之前发出此请求。 但是,当我的网页第一次加载时,我无法让它工作。 我的意思是,axios 请求是在组件加载后完成的。 然后,这将失败: 它在控制台日志中显示了这个错误: ...

5 Laravel Blade:第一次加载页面时获取未定义的变量,刷新时它工作正常

这是我的路线:它工作正常,如果我在 get 方法中执行它,我可以看到转储,并且中间件也工作正常 这是我的控制器:我转储 accountData 并且有值,也转储用户并且是正确的用户,所以 accountData 是正确的,但是在第一次加载页面时变量未定义但如果我刷新它加载正常 这是格式化方法 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM