简体   繁体   中英

Why this javascript not enabling text boxes using check box?

Again in problem Actually I have following jsp code in which I have few text boxes which I have made disabled by using property disabled="disabled". Now problem is each record that I will get from database in each text box using iterator which iterates values added from databse in arraylist.If database return more than one record then using that check box I can enable textboxes but if databse resultset return only one record then I am unable to enable textboxes and throws following ERROR: Message: 'document.f1.chk' is null or not an object Line: 26 Char: 10 Code: 0

<script type="text/javascript">

function enable()
{
    for(i=0;i<document.preapp.chk.length;i++)
        {
        if(document.preapp.chk[i].checked==true)

            {
            document.preapp.id[i].disabled=false;
            document.preapp.vname[i].disabled=false;
            document.preapp.comp[i].disabled=false;
            document.preapp.cont[i].disabled=false;
            document.preapp.wtm[i].disabled=false;
            document.preapp.intime[i].disabled=false;

            }
        else

            if(document.preapp.chk[i].checked==false)
            {
            document.preapp.id[i].disabled=true;
            document.preapp.vname[i].disabled=true;
            document.preapp.comp[i].disabled=true;
            document.preapp.cont[i].disabled=true;
            document.preapp.wtm[i].disabled=true;
            document.preapp.intime[i].disabled=true;
            }
        }
}


</script>

<CENTER><a href="../vm/form.jsp ">Back to Search</a></CENTER>
<form method="post" action="" name="preapp">

<table border="1" align="center" width="100%">
<%

Iterator itr;
try
{
ArrayList al=(ArrayList)request.getAttribute("sq");
int i=0;
for(itr=al.iterator();itr.hasNext();)
    {
i=i+1;


%>
        <tr>
            <td></td><td><input type="checkbox" name="chk"  onclick="enable(this)" ></td></tr></tr>
            <tr><td>Id</td><td><input type="text" name="id" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td>Visitor Name</td><td><input type="text" name="vname" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td>Comapny</td><td><input type="text" name="comp" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td>Contact</td><td><input type="text" name="cont" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td>Meeting Scheduled With</td><td><input type="text" name="wtm" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td>Entry Made On</td><td><input type="text" name="intime" value="<%=itr.next()%>" disabled="disabled" size="100%"></td></tr>
            <tr><td></td>
        </tr>

        <tr>


        </tr>

<%

    }

}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}

%>

How Do solve this problem? please help me out!

It works like charm, except in the case you have only one TR block.

In that case, the .chk has no "length" attribute!

You should consider that case separately:

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;
        }
    }
}

a couple of suggesions: instead of setting the properties of elements to true or false, try using the setAttribute and removeAttribute methods:

document.preapp.id[i].disabled=true;
//replace with:
document.preapp.id[i].setAttribute('disabled','disabled');
//to enable:
document.preapp.id[i].removeAttribute('disabled');

The way you're doing things works fine 99.9% of the time. I haven't seen the above code fail, though (I have had issues with the true/false approach).

Next: the error message you post, contains very useful information: check line 26 of your original code. 'document.f1.chk' is nowhere to be found in your snippet, so I can't check for typo's or other possible problems in your code there.

You're passing the element to the enable function, too. Why then, are you looping through all elements, checking all elems on the page?

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;
}

The last section of the enable function stores the index of the checkbox that was just clicked, so that when the enable function has been clicked before, there's no need to loop through all elements again: enable.previous holds the index of the checkbox that was clicked last time.

Lastly: there are no opening or closing bracket for the else block, and there is an extra line of whitespace. Else works fine without brackets, but only branches one line. In your code, this line is blank: either remove the else, or add brackets.

PS: Perhaps a fiddle would help to get a clear view of the situation?


As Teejay pointed out, in case of unique names, the elements are referenced directly, instead of a nodesList being passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM