0

请有人帮忙。这个表单验证没有触发..这真的让我很沮丧。我是一名 PHP 开发人员而不是 JS,所以我在这方面有点挣扎,尽管这显然很简单。我只是想根据选中的删除框来验证表单。

单击提交时没有任何显示。它只是提交表单,所以它必须返回 true。

function deleteVal(chk) {

    var chk;

    // If the checkbox has been set to delete
    if (chk.checked == "delete") {

        var ok=confirm("You are about to delete the selected images below.\nAre you sure you want to do this?");

        if (ok) {

            // Submit
            return true;

        } else {

            // Don't submit
            return false;

        }

    }

    // Delete box was not ticked so return true and submit form
    return true;

}

</script>

<form action="" method="POST" onSubmit="return deleteVal(delete)">
  <label>Delete images?</label><input type="checkbox" name="delete" value="delete" />
  <input type="submit" name="submit" value="Submit" />
</form>
4

1 回答 1

2

您需要checked像这样正确获取值,看看它是真还是假。

var chk = document.getElementById('delete');

// If the checkbox is checked
if (chk.checked == true) {

然后您不需要将变量传递给函数,但是,您确实需要为您的复选框提供一个 id。

<form action="" method="POST" onSubmit="return deleteVal();">
  <input type="checkbox" name="delete" id="delete"/>
  <input type="submit" name="submit" value="Submit" />
</form>

测试代码 - http://pastebin.com/FdWdeSCN

于 2012-10-18T09:41:49.217 回答