为什么此javascript不使用复选框启用文本框?
又出问题了 实际上,我有以下jsp代码,其中有几个文本框,我使用property disabled=“disabled”禁用了这些文本框。 现在的问题是,我将使用迭代器从每个文本框中的数据库中获取每个记录,迭代器迭代从arraylist中的数据库中添加的值。如果数据库返回多个记录,则使用该复选框,我可以启用文本框,但如果database resultset仅返回一条记录,则我无法启用文本框,并引发以下错误: 消息:“document.f1.chk”为空或不是对象 第26行 字符:10 代码:0为什么此javascript不使用复选框启用文本框?,javascript,jsp,Javascript,Jsp,又出问题了 实际上,我有以下jsp代码,其中有几个文本框,我使用property disabled=“disabled”禁用了这些文本框。 现在的问题是,我将使用迭代器从每个文本框中的数据库中获取每个记录,迭代器迭代从arraylist中的数据库中添加的值。如果数据库返回多个记录,则使用该复选框,我可以启用文本框,但如果database resultset仅返回一条记录,则我无法启用文本框,并引发以下错误: 消息:“document.f1.chk”为空或不是对象 第26行 字符:10 代码:0
函数启用()
{
对于(i=0;i
访客姓名
科帕尼
联系
预定与
于
如何解决这个问题?请帮帮我!它的效果很好,除非你只有一个TR块 在这种情况下,.chk没有“length”属性 你应该单独考虑这个情况:
function enable()
{
if(document.preapp.chk.length == null)
{
disabledState = !document.preapp.chk.checked
document.preapp.id.disabled=disabledState;
document.preapp.vname.disabled=disabledState;
document.preapp.comp.disabled=disabledState;
document.preapp.cont.disabled=disabledState;
document.preapp.wtm.disabled=disabledState;
document.preapp.intime.disabled=disabledState;
} else {
for(i=0;i<document.preapp.chk.length;i++)
{
disabledState = !document.preapp.chk[i].checked
document.preapp.id[i].disabled=disabledState;
document.preapp.vname[i].disabled=disabledState;
document.preapp.comp[i].disabled=disabledState;
document.preapp.cont[i].disabled=disabledState;
document.preapp.wtm[i].disabled=disabledState;
document.preapp.intime[i].disabled=disabledState;
}
}
}
函数启用()
{
if(document.preapp.chk.length==null)
{
disabledState=!document.preapp.chk.checked
document.preapp.id.disabled=禁用状态;
document.preapp.vname.disabled=disabledState;
document.preapp.comp.disabled=禁用状态;
document.preapp.cont.disabled=禁用状态;
document.preapp.wtm.disabled=disabledState;
document.preapp.intime.disabled=disabledState;
}否则{
对于(i=0;i一些建议:不要将元素的属性设置为true或false,而是尝试使用setAttribute
和removeAttribute
方法:
document.preapp.id[i].disabled=true;
//replace with:
document.preapp.id[i].setAttribute('disabled','disabled');
//to enable:
document.preapp.id[i].removeAttribute('disabled');
你做事的方式99.9%都很好,但我没有看到上面的代码失败(我对正确/错误的方法有问题)
下一步:您发布的错误消息包含非常有用的信息:检查原始代码的第26行。“document.f1.chk”在您的代码段中找不到,因此我无法检查您的代码中是否存在打字错误或其他可能的问题
您也在将元素传递给enable函数。那么,为什么要遍历所有元素,检查页面上的所有元素
function enable(elem)
{
var i = document.preapp.indexOf(elem);//
if (elem.checked === true)
{
document.preapp.id[i].removeAttribute('disabled');
//...
}
//functions have properties, exploit them:
if (typeof enable.previous === 'undefined' || enable.previous === i)
{
enable.previous = i;
return true;
}
document.preapp.id[enable.previous].setAttribute('disabled','disabled');
//...
enable.previous = i;
}
enable函数的最后一部分存储了刚才单击的复选框的索引,这样,当以前单击过enable函数时,就不需要再次循环所有元素:enable。previous
保存上次单击的复选框的索引
最后:对于else
块,没有开始或结束括号,并且有一行额外的空白。没有括号,else可以正常工作,但只分支一行。在代码中,此行为空:删除else,或者添加括号
附言:也许一把小提琴会有助于对形势有一个清晰的认识
正如Teejay指出的,如果名称是唯一的,则直接引用元素,而不是传递节点列表。我在代码中看不到名为f1的表单。@sans481:表单名称为preapp@sans481顺便说一句,如果您没有为表单指定名称,它会自动获得“f1”、“f2”等等……但他指定了一个名称“preapp”@Teejay:非常感谢!您的问题解决得非常好。再次感谢。@yatin请单击我的答案旁边的灰色勾号,将我的答案设置为已接受的答案:)@sans481可能它使用ID html属性而不是名称“document.f1.chk”我在更改表单名称之前遇到此错误。
function enable(elem)
{
var i = document.preapp.indexOf(elem);//
if (elem.checked === true)
{
document.preapp.id[i].removeAttribute('disabled');
//...
}
//functions have properties, exploit them:
if (typeof enable.previous === 'undefined' || enable.previous === i)
{
enable.previous = i;
return true;
}
document.preapp.id[enable.previous].setAttribute('disabled','disabled');
//...
enable.previous = i;
}