Javascript getElementById 行为奇怪

发布于 2024-08-14 05:31:34 字数 1523 浏览 10 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

無心 2024-08-21 05:31:34

将您的 y 和 z 变量转换为数字:

var y = +document.getElementById('seatList1').value;
var z = +document.getElementById('seatList2').value;

var startSeat = (z >= y) ? y : z; // or simply startSeat = Math.min(z,y);

这将解决 @Faruz 指出。

Convert your y and z variables to number:

var y = +document.getElementById('seatList1').value;
var z = +document.getElementById('seatList2').value;

var startSeat = (z >= y) ? y : z; // or simply startSeat = Math.min(z,y);

That will fix the problem that @Faruz pointed out.

小梨窩很甜 2024-08-21 05:31:34

我不确定,但也许 startSeat+i 会连接字符串,而不是进行您期望的数学加法。
尝试向屏幕发出警报:

alert(document.getElementById("s"+(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:

alert(document.getElementById("s"+(startSeat+i))); 

Is it the field name?

七月上 2024-08-21 05:31:34

试试这个:

for (var i = startSeat; i < seatNum; i++) {
    if (document.getElementById("s"+i).className == "taken") {
        alert("Selected seat(s) already booked.");
        return false;
    }
}

不要将 i 添加到 startSeat 值来获取座位 ID,而是在循环初始化中直接使用 startSeat 值。我相信发生的事情是您收到off-by-one 错误 因为您的 startSeat 值已经设置,然后您向其中添加了 i ,这使您领先了一位。

Try this:

for (var i = startSeat; i < seatNum; i++) {
    if (document.getElementById("s"+i).className == "taken") {
        alert("Selected seat(s) already booked.");
        return false;
    }
}

Rather than adding i to the startSeat value to get the seat id, use startSeat's value right in the loop initialization. I believe what was happening is that you were getting an off-by-one error since your startSeat value was already set and then you were adding i to it which bumped you ahead by one.

云裳 2024-08-21 05:31:34

当你说循环遍历座位时,我假设你想在到达最大座位后继续从1开始计数?假设 numberOfSeats 是在某处定义的(我看不到它,但你必须在某处有它),你可以这样做:

"s"+((startSeat + i - 1) % numberOfSeats + 1)

所以该行完整:

if (document.getElementById("s"+((startSeat + i - 1) % numberOfSeats + 1)).className=="taken"){

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:

"s"+((startSeat + i - 1) % numberOfSeats + 1)

so the line in full:

if (document.getElementById("s"+((startSeat + i - 1) % numberOfSeats + 1)).className=="taken"){
烟酉 2024-08-21 05:31:34

您真正得到的是字符串连接,所以真正发生的是这样的:

i = 10
seatNum = 1

(seatNum+i) = "110"

尝试使用 parseInt() 函数将变量转换为整数类型:

if (document.getElementById("s"+(parseInt(startSeat)+parseInt(i))).className=="taken")

What you're really getting is string concatenation, so what is really happening is this:

i = 10
seatNum = 1

(seatNum+i) = "110"

Try this using the parseInt() function for casting the variables to integer types:

if (document.getElementById("s"+(parseInt(startSeat)+parseInt(i))).className=="taken")
等风也等你 2024-08-21 05:31:34

在我看来,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.

ι不睡觉的鱼゛ 2024-08-21 05:31:34

您可能会重复太多次。尝试 i < for 循环中的 SeatNum - 1

You could be iterating one too many times. Try i < seatNum - 1 in your for loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文