0

我不知道为什么这不起作用,它上周工作正常,但现在不行了。我想我一定是错误地改变了一些东西,但是在检查了整个事情之后,我什么也找不到。所以,我现在想知道我的代码是否一开始就不是很好。这是delete.js文件:

function deleterecord(form)
    {

    var checkBox = document.edit.checkbox;
    var lenBox = checkBox.length;
    var foundBox = false;

    while( lenBox--> 0 )
        {
            if( checkBox[lenBox].checked === true )
                {
                    foundBox = true;
                    break;
                }
        }

    if( !foundBox )
        {
        alert("Please select a record to delete");
        return;
        }

    var r=confirm("Once deleted, these records are gone forever.\n\n Are you SURE you want to proceed? ");
    if (r==true)
        {
        form.submit();
        }
    else
        {
        alert("Phew! That was close!\n\n No records were deleted.");
        window.location.reload(true);
        }
    return;
    }

...这是 html 表单:

<form action="process_delete.php" method="post" id="edit" name="edit">
<table cellpadding="4px" cellspacing="2px" border="1">

    <tr align="center">
    <td>Aircraft Reg</td>
    <td>Date Installed</td>
    <td>Prefix</td>
    <td>Part Number</td>
    <td>Flight Control</td>
    <td>Lat Position</td>
    <td>Long Position</td>
    <td>Vert Position</td>
    <td>Location</td>
    <td>Material</td>
    <td>OEM/PMA</td>
    <td>Select</td> 
    </tr>

    $result = mysql_query("SELECT * FROM installed $filter ORDER BY aircraft_reg , part_number, date_installed ASC");
    $pagerow = mysql_num_rows($result);
        if ($pagerow > 0 ) 
            {
            while($row = mysql_fetch_array($result))
                {
                echo "<tr>";
                echo "<td>" . $row['aircraft_reg'] . "</td>";
                echo "<td>" . $row['date_installed'] . "</td>";
                echo "<td>" . $row['prefix'] . "</td>";
                echo "<td>" . $row['part_number'] . "</td>";
                echo "<td>" . $row['flight_control'] . "</td>";
                echo "<td>" . $row['lat_position'] . "</td>";
                echo "<td>" . $row['long_position'] . "</td>";
                echo "<td>" . $row['vert_position'] . "</td>";
                echo "<td>" . $row['location'] . "</td>";
                echo "<td>" . $row['material'] . "</td>";
                echo "<td>" . $row['oempma'] . "</td>";
                echo "<td align=\"center\"><input type=\"checkbox\" selected=\"\" id=\"checkbox\" name=\"check_list[]\" value=\"" . $row['id'] . "\"></td>";
                echo "</tr>";
                }
            echo "<tr align=\"center\">";
            echo "<td colspan=\"12\"><input type=\"button\" value=\"Delete Selected\"  style=\"width:150px; height:30px;\" onClick=\"deleterecord(form)\"></td>";
            echo "</tr>";
            }
        else
            {
            echo "<tr><td align=\"center\" colspan=\"12\"><h2>No Records To Display</h2></td></tr>";
            }   
    </table>

</form>

我尝试了不同的变体,document.edit.checkbox;例如document.getElementById["edit"].checkbox;ordocument.getElementById["checkbox"];但我无法让它工作。如果有帮助,这是一个jsfiddle,我删除了 php 位并输入了一些示例值。您可能会说我对 Javascript 很陌生,但我正在尽力而为,非常感谢您提供的任何指导。

4

1 回答 1

2

您想循环遍历所有复选框元素,因此您需要按名称获取它们:

var checkBox = document.getElementsByName('check_list[]');

这是更新的小提琴

于 2012-10-14T10:40:59.960 回答