【问题标题】:<input type="hidden"> working fine in Firefox, but not in Chrome<input type="hidden"> 在 Firefox 中工作正常,但在 Chrome 中不行
【发布时间】:2015-03-13 06:58:08
【问题描述】:

我对这一切都很陌生,如果这是一个愚蠢的问题,请原谅我!当用户单击相关按钮时,我有一些 JavaScript 可以将厘米转换为英寸、厘米转换为米、米转换为英寸等。我想隐藏不相关的按钮。即当用户选择以英寸为单位的输入单位时,“厘米到英寸”是无关紧要的,应该隐藏。

我的代码在 Firefox 中运行良好,只有一半在 chrome 中运行。当用户从下拉列表中选择单位时,它应该适当地显示/隐藏按钮。但在 Chrome 中,它仅在您第一次选择单位时起作用,并且在选择其他单位时不会更新。

这是 HTML:

            <select id="unit" onchange="TriangleArea()" class="unitOut">
                <option value="" id="unitCSS" onClick="CreateButtons()">Select Unit...</option>
                <option value="CM" onClick="CreateButtons()">Centimeters</option>
                <option value="M" onClick="CreateButtons()">Meters</option>
                <option value="Inch" onClick="CreateButtons()">Inches</option>
            </select>

这里是 JavaScript:

function CreateButtons(){
        if (unit == "CM" )
        {
            document.getElementById("button1").setAttribute("type", "button"); // shows button1 (CM to Inch)
            document.getElementById("button2").setAttribute("type", "hidden"); // hides button2 (Inch to CM)
            document.getElementById("button3").setAttribute("type", "button"); // shows button3 (CM to M)
            document.getElementById("button4").setAttribute("type", "hidden"); // hides button4 (M to CM)
            document.getElementById("3cube").style.visibility="visible";
        }
        else if (unit =="Inch")
        {
            document.getElementById("button1").setAttribute("type", "hidden"); 
            document.getElementById("button2").setAttribute("type", "button"); 
            document.getElementById("button3").setAttribute("type", "hidden"); 
            document.getElementById("button4").setAttribute("type", "hidden");
            document.getElementById("3cube").style.visibility="visible";
        }
        else if (unit =="M")
        {
            document.getElementById("button1").setAttribute("type", "hidden"); 
            document.getElementById("button2").setAttribute("type", "hidden"); 
            document.getElementById("button3").setAttribute("type", "hidden"); 
            document.getElementById("button4").setAttribute("type", "button");
            document.getElementById("3cube").style.visibility="visible";
        }           
        else if (unit =="")
        {
            document.getElementById("button1").setAttribute("type", "button"); 
            document.getElementById("button2").setAttribute("type", "button"); 
            document.getElementById("button3").setAttribute("type", "button"); 
            document.getElementById("button4").setAttribute("type", "button");  
            document.getElementById("3cube").style.visibility="hidden";
        }
    }

unit 变量在文档正文中以全局范围声明。它设置为&lt;select id="unit"&gt; 的值,代码为unit = document.getElementById("unit").value,并且工作正常。那么为什么 Firefox 每次用户更改单位选项时都会隐藏/显示按钮,而 Chrome 只会在第一次显示正确的按钮而不在单位更改时更新?

解决了! onchange="CreateButtons()" 需要是 &lt;select&gt; 的属性,而不是 &lt;option&gt;。感谢您的所有时间和帮助!

【问题讨论】:

  • 对不起,我应该澄清一下。 unit 变量设置为选定&lt;option&gt; 的值,即无,CM、M 或英寸。使用代码unit = document.getElementById("unit").value

标签: javascript google-chrome input


【解决方案1】:

使用样式 -> display 属性来隐藏和显示按钮。不要让它们隐藏起来。喜欢,

document.getElementById("button1").style.display='none'; // hides button1
document.getElementById("button1").style.display='block'; // shows button1

display:none 也隐藏了按钮占用的空间。

并且不要对每个选项都使用“onclick”,

            <select id="unit" onchange="CreateButtons()" class="unitOut">
                <option value="" id="unitCSS">Select Unit...</option>
                <option value="CM">Centimeters</option>
                <option value="M">Meters</option>
                <option value="Inch">Inches</option>
            </select>

像这样分配“单位”变量,

function CreateButtons(){
        var unit = document.getElementById('unit').value;
        // do your rest of the code ...
        if (unit == "CM" )
        {
           // your code ...
        }
}

希望它有效,如果您需要更多帮助,请告诉我。谢谢。

【讨论】:

  • 我绑定了这个,但它仍然不起作用` if (unit == "CM" ) { document.getElementById("button1").style.display='block'; // 显示 button1(CM 到英寸) document.getElementById("button2").style.display='none'; // 隐藏 button2(英寸到 CM) document.getElementById("button4").style.display='none'; // 隐藏 button4(M 到 CM) }` 还有这个 HTML:&lt;button id="button1" style="display: none;" onClick="CentiToInch()"&gt;CM to Inch&lt;/button&gt;
  • 这是非常基本的 javascript,应该可以在 Chrome 中运行。请检查一下您的 Google Chrome 中是否启用了 javascript。请查看编辑后的答案。
  • 是的,JavaScript 已启用。别担心,这不是重要的工作!感谢您的帮助!
  • 是的,因为您没有分配在条件下使用的“单位”变量。请检查已编辑的答案。
  • 我添加了您建议将“单位”变量设置为&lt;select id="unit"&gt; 的值并为“单位”发出警报的行。它仅在第一次而不是后续时间发出警报,这表明选择一个选项未运行 CreateButtons() 函数。有趣的是,之前的更改现在已经在 Firefox 中“破坏”了它,因此 Firefox 和 Chrome 现在的行为方式相同。
【解决方案2】:

您必须像使“3cube”可见一样显示和隐藏按钮,即

隐藏按钮:

document.getElementById('button1').style.visibility = 'hidden';

显示按钮:

document.getElementById('button1').style.visibility = 'visible';

【讨论】:

    【解决方案3】:

    建议您不要使用“Style.Visibility”属性,而是使用 'Style.display' 属性用于隐藏/显示按钮,正如 @balaG 之前提到的。

    这是因为,按钮一旦设置为,

    Style.Visibility=false;  
    

    你将无法通过使用在 DOM 中再次找到它,

    document.getElementByID('some_id');
    

    希望这会有所帮助!

    【讨论】:

    • 尝试从下拉列表中找到选定的值。即,在函数 'CreateButtons()' 中的 if else 阶梯中使用类似 If($('#unit').val()=="CM") 尝试提醒单位和 $('#unit').val () 在你的函数 'CreateButtons()' 中查看天气,你得到了想要的值
    【解决方案4】:

    &lt;option onclick=&gt; 不适用于 Chrome。您应该收听&lt;select&gt; 元素的“更改”事件。

    【讨论】:

    • 我尝试将其更改为onchange,但这也没有解决它。不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 2016-07-13
    • 2016-10-06
    • 2014-08-12
    • 2013-05-29
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多