gpt4 book ai didi

javascript - 表单验证 - 使用 Javascript 的复选框

转载 作者:太空宇宙 更新时间:2023-11-04 16:11:07 26 4
gpt4 key购买 nike

当用户单击“确认”复选框时,我尝试验证一些信息。问题是这是从 Access 数据库中提取的数据,因此表单字段 ID 具有变量。这是我用来触发事件的复选框:

<INPUT TYPE="checkbox" id="checkconfirm<%=j%>" NAME="confirm<%=j%>" align="center" value="Yes" onclick="ConfirmBoxChecked(this.form.confirm<%=j%>.name,this.form.confirmYear<%=j%>.value,this.form.confirmStatus<%=j%>.value)">

其中 j 是从访问数据库中提取的每条记录(因此第一条记录复选框是 checkconfirm1,第二条记录是 checkconfirm2,等等)。

confirmYear 和 confirmStatus 是我正在检查的两条数据。

我的 javascript onclick 函数是这样的:

function ConfirmBoxChecked(confirmbox, yearz, statuz) {
var datecheck = new Date;
//alert("Working! " + yearz + " working " + confirmbox + " working " + statuz + " working " + datecheck.getFullYear() + " working ");
var boxname = "\'" + confirmbox + "\'"
alert(boxname);
if (statuz=="P" && (yearz < datecheck.getFullYear())) {
alert("This fleet was delivered in " + yearz + " but is still listed as 'Potential Order'.  Please edit the year or status of these vehicles.");
document.getElementByID(boxname).checked=false;
}
if (statuz=="O" && (yearz < datecheck.getFullYear())) {
alert("This fleet was delivered in " + yearz + " but is still listed as 'On Order'. Please edit the year or status of these vehicles.");
return false;
}
if (statuz=="A" && yearz > datecheck.getFullYear() - 1) {
alert("You have entered vehicles with status=''Active'', but with a model year of " + yearz + ". Your responses should reflect the status of these vehicles on 1/1/" + daty.getFullYear() + ".\nVehicles being delivered in " + daty.getFullYear() + " or in later years should be ''On Order'' or ''Potential'' based on the contract status. Please correct the year or status of these vehicles.");
return false;
}
}

在我看来,验证工作正常 - IF 正确触发后的警报。但是我希望在该警报触发后取消选中该复选框。我不知道该怎么做。


好的,这是解决它的方法。我没有提取名称,而是提取了元素。所以输入代码看起来像:

<INPUT TYPE="checkbox" id="checkconfirm<%=j%>" NAME="confirm<%=j%>" align="center" value="Yes" onClick="ConfirmBoxChecked(this,this.form.confirmYear<%=j%>.value,this.form.confirmStatus<%=j%>.value)">

然后函数代码如下:

function ConfirmBoxChecked(confirmbox, yearz, statuz) {
var datecheck = new Date;
//alert("Working! " + yearz + " working " + confirmbox + " working " + statuz + " working " + datecheck.getFullYear() + " working ");
//var boxname = "\"" + confirmbox + "\""
//alert(boxname);
if (statuz=="P" && (yearz < datecheck.getFullYear())) {
alert("This fleet was delivered in " + yearz + " but is still listed as 'Potential Order'.  You must edit the year or status of these vehicles before you can confirm.");
confirmbox.checked = false; }
if (statuz=="O" && (yearz < datecheck.getFullYear())) {
alert("This fleet was delivered in " + yearz + " but is still listed as 'On Order'. You must edit the year or status of these vehicles before you can confirm.");
confirmbox.checked = false; }
if (statuz=="A" && yearz > datecheck.getFullYear() - 1) {
alert("You have entered vehicles with status=''Active'', but with a model year of " + yearz + ". Your responses should reflect the status of these vehicles on 1/1/" + daty.getFullYear() + ".\nVehicles being delivered in " + daty.getFullYear() + " or in later years should be ''On Order'' or ''Potential'' based on the contract status. Please correct the year or status of these vehicles.");
confirmbox.checked = false; }

}

而且有效!

最佳答案

您将复选框的 name 属性作为函数中的 confirmbox 参数传递。然后在您的代码中调用 getElementById(confirmbox),这不是 id。尝试传递 id 属性:

<INPUT TYPE="checkbox" id="checkconfirm<%=j%>" NAME="confirm<%=j%>" align="center" value="Yes" onclick="ConfirmBoxChecked(this.id,this.form.confirmYear<%=j%>.value,this.form.confirmStatus<%=j%>.value)">

关于javascript - 表单验证 - 使用 Javascript 的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com