简体   繁体   English

为什么HTML复选框功能仅在IE中有效,而在Firefox或Chrome中不起作用?

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

I'm debugging a JavaScript/JSP/Struts app, it has a checkbox, for advanced search, when it's checked, other 2 items are supposed to show up on the page for user to enter more info, yet this only works in IE, but not Firefox or Chrome, no response at all when it's checked on the other 2 browsers, why ? 我正在调试一个JavaScript / JSP / Struts应用程序,它具有一个复选框,可以进行高级搜索,如果选中该复选框,则应该在页面上显示其他2个项目供用户输入更多信息,但这仅在IE中有效,但不是Firefox或Chrome,在其他2种浏览器上进行检查时完全没有响应,为什么? And how to make it work in all browsers ? 以及如何使其在所有浏览器中都能正常工作?

在此处输入图片说明

<script type="text/javascript">
    function checkAdvSearch(checked) {      
        if(checked) {
            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("searchLOB").style.display = 'none';

            document.getElementById("searchTerm2").value = '';
            document.getElementById("searchField2").value = 'clientName';
            document.getElementById("searchStatus").value = '';
            document.getElementById("searchLOB").value = '';
        }
    }
</script>

...
<!-- for advanced search -->
  <td Valign=top width=300>
    <input type="checkbox" name="advSearch" onclick="checkAdvSearch(this.checked);" tabindex="5"/>Advanced Search
    <html:text property="searchTerm2" value="" style="display:none" tabindex="6"/>
  </td>
  <td Valign=top width=178>
    <html:select property="searchField2" onchange="showOptions2(this.form)" value= "" style="display:none" tabindex="7">
      <html:option value="clientName">Insured Name</html:option>
      <html:option value="policy">Policy Number</html:option>
        ...
    </html:select>
  </td>
...

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. 由于我从未见过声明html元素的格式,因此我很可能会猜测您的问题就在这里,最可能的原因是attritube的值未在某些浏览器中正确转换为id。 You may want to stick to the standard html tags for web development. 您可能要坚持使用标准html标签进行Web开发。

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. 要验证这是Firefox中的问题,请尝试使用ctrl-shift-k打开控制台,然后单击高级复选框,应该会收到以下消息。

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

After some research I found the answer, added "styleId" to the following solved the problem : 经过一番研究,我找到了答案,在下面的问题中添加了“ styleId”:

<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. 处理后的styleId =“ xyz”将被转换为id =“ xyz”,将由document.getElementById()进行标识,否则将因为其中没有ID而导致错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript onclick仅适用于chrome,不适用于Firefox或IE - Javascript onclick only works in chrome, not in firefox or IE Jquery Javascript仅适用于chrome和firefox,但不适用于IE - Jquery Javascript only works on chrome and firefox, but not on IE PHP脚本和HTML - 这适用于Firefox和Chrome,但不适用于IE - PHP Script and HTML - This works in Firefox and Chrome, but not IE JavaScript函数不适用于Chrome和IE,但适用于FireFox - JavaScript function not working on Chrome & IE but works on FireFox 该功能适用​​于Chrome,但在IE和Firefox中无法正常运行 - Function works fine with chrome but not in IE and Firefox 强制仅数字js函数在Firefox中不起作用,但在Chrome和IE中可以正常工作 - Force numeric only js function doesn't work in Firefox but in Chrome & IE works fine 为什么JS功能在IE9中无法正常工作? 但在Chrome和Firefox中可以正常工作 - Why JS function does not work properly in IE9? but works fine in Chrome and Firefox 简单的GET rest服务只能在IE(或Postman)中使用,而不能在Chrome中使用,而不能在Firefox中使用 - Simple GET rest service works in IE only ( or Postman ) , but not in Chrome , not in Firefox 始终在chrome / firefox中运行的脚本在IE中只能运行一次 - script always working in chrome / firefox works only once in IE html5 canvas中的OffsetX可用于IE,Safari,Chrome,但不适用于Firefox - OffsetX in html5 canvas works with IE, Safari, Chrome but not with Firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM