lundi 18 novembre 2019

Struts2: checkbox stuck as selected

I am working on a website made with Struts2 and I am facing a problem with a checkbox.

I have a dropdown with a list of companies. If the user does not find his company in the list, he can click a checkbox to replace the dropdown with a text field. Here is the code:

Extract of the JSP:

<tr>
    <td align="right" width="49%">
        <table>
            <sj:select id="company_select" name="company_select" list="company_list" .../>
        </table>
    </td>
    <td colspan="2" align="left">
        <table>
            <s:hidden name="company_text" id="company_text" label="Company Type" disabled="true"/>
        </table>
    </td>
</tr>
<tr>
    <td align=left>
        <s:checkbox name="checkboxCompanyText" id="checkboxCompanyText" label="Company Not in the List"
                                            fieldValue="false" onchange="showCompanyText()"/>
    </td>
    <td></td>
</tr>

The JS function:

function showCompanyText() {
        if(document.querySelector('#checkboxCompanyText').checked = "true") {
            document.getElementById('company_text').type = "visible";
            document.getElementById('company_text').disabled = false;
            document.getElementById('company_select').style.display = "none";
        }else{
            document.getElementById('company_text').type = "hidden";
            document.getElementById('company_text').disabled = true;
            document.getElementById('company_select').style.display = "block";
        }
    }

When the page loads, the checkbox is unchecked, the dropdown is here and the text input field is not visible. Good. When I check the checkbox, the dropdown goes away and the text field appears. Again: good. But when I try to uncheck the checkbox, it just remains checked, impossible to uncheck it.

If anyone has an idea of what's going on, you are welcome.




Aucun commentaire:

Enregistrer un commentaire