问题描述:

I want to enable the disabled text field based on selection of checkbox, so below is the JavaScript code to enable the text field based on selection of check box, which is not working for me and am using the struts 1.3 HTML tags

if(document.getElementsByName("cad.question7").checked = 'true'){

document.getElementsByName("cad.question7.answer8").disabled = 'false';

}

<html:checkbox value="Y" property="cad.question7" onclick="javascript:enableText7();" />

<bean:message key="cm.assessments.cad.question.7.a" />

<html:text size="10" disabled="true" property="cad.question7.answer8" tabindex="68" />

网友答案:

getElementsByName returns a HTMLCollection.

To access each element inside the collection either iterate through them or if you only have one, access the first one, similar to this:

document.getElementsByName("cad.question7.answer8")[0].disabled = false;

DEMO - Access first item in HTMLCollection


If you expect multiple matches iterate through them similar to this:

var index;
var elements = document.getElementsByName("cad.question7.answer8");
var count = elements.length;

for(index = 0; index < count; index++){
    elements[index].disabled = false;
}

DEMO - Iterate through all items in the HTMLCollection


In addition when executing a conditional evaluation you use == or === as a single = will assign the right value to the left and not compare them with each other.

Your complete code may look similar to this, assuming only a single match is expected:

if(document.getElementsByName("cad.question7")[0].checked){
    document.getElementsByName("cad.question7.answer8")[0].disabled = false;
}

DEMO - Example of new code above


网友答案:
  • In the if-condition, you want to use the == or === comparison operator, not the = assignment operator. But comparing against booleans is senseless, just use the boolean itself (or its negation).
  • document.getElementsByName returns not a single element, but a NodeList. You will need to iterate over it, or - for simplicity - access its first item.
  • Both the checked and disabled properties are boolean. You should not compare/assign string to them, but booleans.
if (document.getElementsByName("cad.question7")[0].checked) {
    document.getElementsByName("cad.question7.answer8")[0].disabled = false;
}

You might even go with this:

var question = document.getElementsByName("cad.question7")[0],
    answer = document.getElementsByName("cad.question7.answer8")[0];
answer.disabled = !question.checked;
网友答案:

It should be boolean. disabled takes boolean value and also getElementsByName return multiple elements in an array.

Issue with your disabled="false" is that it still leaves the attrbute disabled in your text input element.

Try this:-

if(document.getElementsByName("cad.question7")[0].checked){

    document.getElementsByName("cad.question7.answer8")[0].disabled = false;
}

References:-

getElementsByName

disabled

网友答案:

Below JavaScript codes you can use, your wish.,

But i would suggest you to use this (First one):

if(document.getElementsByName("cad.question7").checked){
    document.getElementsByName("cad.question7.answer8")[0].disabled = false;
}

or

if(document.getElementsByName("cad.question7").checked == true){
    document.getElementsByName("cad.question7.answer8").disabled = false;
}

or

var chkTest = document.getElementsByName("cad.question7");
if(chkTest.checked){
    document.getElementsByName("cad.question7.answer8").disabled = false;
}
相关阅读:
Top