[英]Resolving TypeError: event is undefined
I am attempting to take a user's postal code to request the representatives of the locality. 我试图用一个用户的邮政编码来请求当地的代表。 For troubleshooting purposes, I am attempting to log the console of both the postal code and the url including the postal code.
出于疑难解答的目的,我试图记录邮政编码和网址的控制台,包括邮政编码。
$(document).ready(function() { //Wait until user submits postal code (ie. K2H6G7) $("form").on("submit", function(event) { var request = new XMLHttpRequest(); event.preventDefault(); var searchWord = $("#search").val(); //Take postalcode and apply to url var url = "https://represent.opennorth.ca/postcodes/" + searchWord + "/?format=apibrowser"; console.log(searchWord); console.log(url) request.open('GET', url, true); request.onreadystatechange = function() { if (request.readyState === 4) { if (request.status === 200) { //Connect to API, create cards for each politician instance var data = JSON.parse(this.response); data.representatives_centroid.forEach(representatives_centroid => { const card = document.createElement('div'); card.setAttribute('class', 'card'); const img = document.createElement('img'); img.src = representatives_centroid.photo_url; const p1 = document.createElement('p'); p1.textContent = `$ { representatives_centroid.party_name } $ { representatives_centroid.elected_office } $ { representatives_centroid.first_name } $ { representatives_centroid.last_name } ` const p2 = document.createElement('p'); p2.textContent = `$ { representatives_centroid.district_name } ` //Add data to card container.appendChild(card); card.appendChild(img); card.appendChild(p1); card.appendChild(p2); }); } else { console.log('error'); } } } request.send(); } ()); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="form"> <form type="text" class="form-inline" role="form"> <label for="address" name="postalcode">Enter an address or postal code</label> <input id="search" placeholder="Enter an address or postal code" type="text"> <button id="go"> <span class="glyphicon glyphicon-search"></span> Find </button> </form> </div>
The error message I receive is "TypeError: event is undefined". 我收到的错误消息是“TypeError:event is undefined”。 Am I missing something obvious?
我错过了一些明显的东西吗
I'm not sure but what is that about type in the form? 我不确定,但表格中的类型是什么?
the type="text" is for inputs, and I think you need an action="/route/" instate of type in order to have an event and could prevent it. type =“text”用于输入,我认为你需要一个action =“/ route /”类型的instate才能有一个事件并且可以阻止它。
Regards.- 问候。-
Change your closing brackets 更改结束括号
$(document).ready(function() {
//Wait until user submits postal code (ie. K2H6G7)
$("form").on("submit", function(event) {
var request = new XMLHttpRequest();
event.preventDefault();
var searchWord = $("#search").val(); //Take postalcode and apply to url
var url = "https://represent.opennorth.ca/postcodes/" + searchWord + "/?format=apibrowser";
console.log(searchWord);
console.log(url)
request.open('GET', url, true);
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
//Connect to API, create cards for each politician instance
var data = JSON.parse(this.response);
data.representatives_centroid.forEach(representatives_centroid => {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const img = document.createElement('img');
img.src = representatives_centroid.photo_url;
const p1 = document.createElement('p');
p1.textContent = `${representatives_centroid.party_name} ${representatives_centroid.elected_office} ${representatives_centroid.first_name} ${representatives_centroid.last_name}`
const p2 = document.createElement('p');
p2.textContent = `${representatives_centroid.district_name}`
//Add data to card
container.appendChild(card);
card.appendChild(img);
card.appendChild(p1);
card.appendChild(p2);
});
} else {
console.log('error');
}
}
}
request.send();
//THESE //
// Change from }()); to });
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.