Why html checkbox function only works in IE but not in Firefox or Chrome?

依然范特西╮ 提交于 2019-12-06 16:24:43

I believe this simplified code sample here works as you intended it:

<html>
<body>

<script type="text/javascript">
    function checkAdvSearch(checked) {
        console.log("Test");
        if(checked == 1) {
            document.getElementById("searchTerm2").style.display = '';
            document.getElementById("searchField2").style.display = '';
        }else {
            document.getElementById("searchTerm2").style.display = 'none';
            document.getElementById("searchField2").style.display = 'none';


            document.getElementById("searchTerm2").value = '';
            document.getElementById("searchField2").value = 'Client Name';
        }
    }
</script>

    <input type="checkbox" name="advSearch" onclick="checkAdvSearch(this.checked);" />Advanced Search
    <input type="text" id="searchTerm2" value="" />
    <select id="searchField2" value= "clientName" >
        <option>Client Name</option>
        <option>Policy Number</option>
    </select>
</body>
</html>

As I have never seen the format for declaring html elements, I would hazard a guess that your problem lies there, and the most likely cause is that the value attritube is not getting translated into id correctly on some browsers. You may want to stick to the standard html tags for web development.

To verify this is your problem in Firefox, try opening the console using ctrl-shift-k and you should get the following message when you click the advanced checkbox.

TypeError: document.getElementById(...) is null

After some research I found the answer, added "styleId" to the following solved the problem :

<html:text property="searchTerm2" styleId="searchTerm2" value="" style="display:none" tabindex="6"/>

<html:select property="searchField2" styleId="searchField2" onchange="showOptions2(this.form)" value= "" style="display:none" tabindex="7">

styleId="xyz" after processing will be turned into Id="xyz" which will be identified by document.getElementById(), otherwise it will cause error because there is no Id in it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!