0

假设我在一个名为photo/delete.php. 此表单删除用户指定的图像。就是这样,是这样的:

<form action="?" method="post">
 <input type="checkbox" name="confirmDelete" value="confirm" />    
 <input type="submit" value="delete" />
</form>

因此,此表单包含一个确认复选框,必须勾选该复选框以确保删除图像。如何根据其内容动态选择将此表单发布到哪个页面?

例如,如果未选中复选框,但单击了提交按钮,我想留在同一photo/delete.php页面上并显示错误,因为他们可能确实想删除图像而只是忘记勾选框.

但除此之外,如果一切都成功并且勾选了复选框,我想将它发布到另一个页面,home.php因为留在刚刚删除的图像的同一页面上是没有意义的。

我该如何实施?

4

1 回答 1

0

你可以试试这样的

HTML:

<form action="delete.php" method="post">
    <input type="checkbox" name="confirmDelete" value="confirm" />    
    <input id="btn_delete" type="submit" value="delete" />
</form>

JS:

window.onload = function(){
    document.getElementById('btn_delete').onclick = function(e){
        var checkBox = document.getElementsByName('confirmDelete')[0];
        if(!checkBox.checked) {
            if(e && e.preventDefault) {
                e.preventDefault();
            }
            else if(window.event && window.event.returnValue) {
                window.eventReturnValue = false;
            }
            alert('Please check the checkbox!');
        }
    };
};

演示。

于 2013-09-06T10:59:31.483 回答