如何使用 onblur 在相邻单元格上调用方法?

How to call method on adjacent cell with onblur?

我正在尝试向用户输入数据的 table 添加验证。我要收集的信息是轮班时间,轮班开始和结束之间不能有间隙(例如 3:59,4:00 是我要找的)。当用户编辑班次时间时,我希望能够查看相邻单元格并自动调整该单元格时间。

因此,如果二班开始时间从 14:00 变为 13:00,我应该同时查看二班结束时间和一班结束时间并相应地调整这些时间并继续寻找直到不再做任何更改。

有没有办法在相邻单元格上调用方法?

这是用 HTML、经典 ASP 和 Javascript 写的。

<table border='1' style='border-collapse:collapse;'>
    <tr>
        <th>Name</th>
        <th>Start</th>
        <th>End</th>
    </tr>
    <tr>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl0Name' Value='First' onBlur='updateGrid("Name", cntl0Name, "0", "First", "06:00", "13:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl0Start' Value='06:00' onBlur='updateGrid("Start", cntl0Start, "0", "First", "06:00", "13:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl0End' Value='13:59' onBlur='updateGrid("End", cntl0End, "0", "First", "06:00", "13:59", 1);'></td>
    </tr>
    <tr>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl1Name' Value='Second' onBlur='updateGrid("Name", cntl1Name, "1", "Second", "14:00", "21:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl1Start' Value='14:00' onBlur='updateGrid("Start", cntl1Start, "1", "Second", "14:00", "21:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl1End' Value='21:59' onBlur='updateGrid("End", cntl1End, "1", "Second", "14:00", "21:59", 1);'></td>
    </tr>
    <tr>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl2Name' Value='Third' onBlur='updateGrid("Name", cntl2Name, "2", "Third", "22:00", "05:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl2Start' Value='22:00' onBlur='updateGrid("Start", cntl2Start, "2", "Third", "22:00", "05:59", 1);'></td>
        <td style='width: auto;'><Input style='border-style:none; width: auto;' Type='Text' Name='cntl2End' Value='05:59' onBlur='updateGrid("End", cntl2End, "2", "Third", "22:00", "05:59", 1);'></td>
    </tr>
</table>

是的,您可以使用 previousSibling 和 nextSibling DOM 属性 来访问相邻的单元格。

this.previousSibling.value 会给你以前的单元格值和 this.nextSibling.value 将为您提供下一个单元格值。