如何删除需要至少1条记录的删除确认?

时间:2012-02-13 15:49:45

标签: php javascript

我需要帮助才能删除确认,删除至少需要1条记录。 我仍然对制作它感到困惑。我认为我的javascript代码有问题。任何帮助将不胜感激。感谢

这是php代码:

enter code here

<script src="javascript.js" type="text/javascript"></script>
<?php
echo"<form method=POST action='action.php?act=delete'>
     <input type=checkbox name='checkbox[]' value='1'>1
     <input type=checkbox name='checkbox[]' value='2'>2
     <input type=checkbox name='checkbox[]' value='3'>3
     <input type=submit value=Delete onClick='return del_confirm();'></form>";
?>

这是javascript代码:

enter code here

function del_confirm()
{
var msg=confirm('Are you sure?');
var c=document.getElementsByName('checkbox[]');
if(msg)
{
    for(i=0;i<c.length;i++)
    {
    if(c[i].checked)
    {
        return true;
    }
    else
    {
        alert("Select minimum of 1 record to be deleted!");
        return false;
    }
    }
}
else
{return false;}
    }

1 个答案:

答案 0 :(得分:3)

你的逻辑有点偏离:

function del_confirm() {
    var msg = confirm('Are you sure?');
    var c = document.getElementsByName('checkbox[]');
    if(msg) {
        for(i = 0; i < c.length; i++) {
            if(c[i].checked) {
                return true;
            }
        }
        // This has to be outside the for loop,
        // that way it only gets here if every box is not checked
        alert("Select minimum of 1 record to be deleted!");
        return false;
    } else {
        return false;
    }
}​

你在for循环中有警报,所以第一个未经检查的框为函数返回false。

示例小提琴:http://jsfiddle.net/FfkvW/