Struts2:复选框处于选中状态

我正在使用Struts2创建的网站上,但复选框出现问题。

我有一个公司列表的下拉列表。如果用户在列表中找不到他的公司,则可以单击复选框以将下拉列表替换为文本字段。这是代码:

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>

JS功能:

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

页面加载时,未选中此复选框,此处为下拉列表,并且文本输入字段不可见。好。当我选中复选框时,下拉列表消失,并显示文本字段。再次:好。但是,当我尝试取消选中该复选框时,它始终保持选中状态,无法取消选中它。

如果有人对正在发生的事情有任何想法,欢迎您。

solabc 回答:Struts2:复选框处于选中状态

有时,您的错误非常明显,以致您看不到它。我真诚地花了几分钟查看代码,试图弄清发生了什么。原来,解决方案是将if(document.querySelector('#checkboxCompanyText').checked = "true")转换为if(document.querySelector('#checkboxCompanyText').checked === true)。有时候我讨厌自己。

本文链接:https://www.f2er.com/3081398.html

大家都在问