Javascript getElementById 行为奇怪
我使用 for 循环来循环访问一些具有起始值的元素(飞机上的座位)。
这是:
SeatNum - 要循环的座位数
startSeat - 开始骑行的座位
我从“onsubmit”表单中调用该函数。
当我尝试获取具有“s1”“s2”“s3”等 id 命名约定的元素...“s45”“s46”等...基于添加到的循环计数器时,问题出现在 for 循环中起始座位。从 0(起始座位)数到 SeatNum(多少个座位)。
知道为什么 by id 不能正确解析吗?除了 for 循环中的最后一个之外,所有其他都工作正常。
是的,我是编程新手,所以我可能没有最佳实践,请在风格上原谅。
function check() {
var startSeat;
var fName = document.getElementById('fName').value
var lName = document.getElementById('lName').value
var address = document.getElementById('address').value
var city = document.getElementById('city').value
var state = document.getElementById('state').value
var zip = document.getElementById('zip').value
var phone = document.getElementById('phone').value
var seatNum = document.getElementById('seatNumber').value
var y=document.getElementById('seatList1').value;
var z=document.getElementById('seatList2').value;
if (z >= y) {
startSeat = y;
}
else {
startSeat = z;
}
if ( (fName == "") || (lName == "") || (address == "") || (phone == "") || (zip == "") || (state == "") || (city == "") ) {
alert("You must fully complete the form");
return false;
}
for (var i = 0; i < seatNum; i++) {
if (document.getElementById("s"+(startSeat+i)).className=="taken"){
alert("Selected seat(s) already booked.");
return false;
}
else {
continue;
}
}
}
I'm using a for loop to cycle through some elements with a starting value (seats on a plane).
Here it is:
seatNum - Number of seats to be cycled through
startSeat - seat to begin cycling
I'm calling the function from a form "onsubmit".
The problem comes in the for loop when i try and go get elements with an id naming convention of "s1" "s2" "s3" etc... "s45" "s46" etc... based on the loop counter added to the starting seat. Counting from 0(starting seat) up to seatNum(how many seats).
any idea why by id isn't resolving properly? All others work fine except the last one inside the for loop.
Yes I'm new to programming so i probably don't have the best practices, please be forgiving stylistically.
function check() {
var startSeat;
var fName = document.getElementById('fName').value
var lName = document.getElementById('lName').value
var address = document.getElementById('address').value
var city = document.getElementById('city').value
var state = document.getElementById('state').value
var zip = document.getElementById('zip').value
var phone = document.getElementById('phone').value
var seatNum = document.getElementById('seatNumber').value
var y=document.getElementById('seatList1').value;
var z=document.getElementById('seatList2').value;
if (z >= y) {
startSeat = y;
}
else {
startSeat = z;
}
if ( (fName == "") || (lName == "") || (address == "") || (phone == "") || (zip == "") || (state == "") || (city == "") ) {
alert("You must fully complete the form");
return false;
}
for (var i = 0; i < seatNum; i++) {
if (document.getElementById("s"+(startSeat+i)).className=="taken"){
alert("Selected seat(s) already booked.");
return false;
}
else {
continue;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将您的 y 和 z 变量转换为数字:
这将解决 @Faruz 指出。
Convert your
y
andz
variables to number:That will fix the problem that @Faruz pointed out.
我不确定,但也许 startSeat+i 会连接字符串,而不是进行您期望的数学加法。
尝试向屏幕发出警报:
是字段名称吗?
I'm not sure, but maybe startSeat+i concats the strings and not doing the mathematical add you expect.
Try alerting to screen:
Is it the field name?
试试这个:
不要将
i
添加到startSeat
值来获取座位 ID,而是在循环初始化中直接使用startSeat
值。我相信发生的事情是您收到off-by-one 错误 因为您的startSeat
值已经设置,然后您向其中添加了i
,这使您领先了一位。Try this:
Rather than adding
i
to thestartSeat
value to get the seat id, usestartSeat's
value right in the loop initialization. I believe what was happening is that you were getting an off-by-one error since yourstartSeat
value was already set and then you were addingi
to it which bumped you ahead by one.当你说循环遍历座位时,我假设你想在到达最大座位后继续从1开始计数?假设 numberOfSeats 是在某处定义的(我看不到它,但你必须在某处有它),你可以这样做:
所以该行完整:
When you say cycle through the seats, I assume you want to continue counting from 1 after you reach the maximum seat? Assuming that numberOfSeats is defined somewhere (I couldn't see it, but you must have it somewhere) you can do this:
so the line in full:
您真正得到的是字符串连接,所以真正发生的是这样的:
尝试使用
parseInt()
函数将变量转换为整数类型:What you're really getting is string concatenation, so what is really happening is this:
Try this using the
parseInt()
function for casting the variables to integer types:在我看来,startSeat 是一种字符串类型。尽管 JavaScript 是无类型的,但 DOM 对象的值将默认为字符串。因此,您得到的是串联而不是加法。
使用 CMS 编写的内容。这应该可以解决你的问题。
Looks to me like startSeat is a string type. Even though JavaScript is type-less, the value of a DOM object is going to default as a string. So, you're getting a concatenation instead of addition.
Use what CMS wrote. That should fix your problem.
您可能会重复太多次。尝试
i < for 循环中的 SeatNum - 1
You could be iterating one too many times. Try
i < seatNum - 1
in your for loop