【问题标题】:Is there a way of making text hidden in an input box?有没有办法让文本隐藏在输入框中?
【发布时间】:2020-05-14 03:44:00
【问题描述】:

我有这个度数转换器的代码,它工作得很好。但我担心的一个问题是,当我选择一个度数时,比如摄氏温度,我输入我的数字并按下按钮,它会给我开尔文和华氏温度。但是假设我想在开尔文中有另一个温度,我在框中输入我的数字并按计算,它会生成华氏温度和摄氏温度,但它会将之前的开尔文值留在输入框中。我想要的是,根据您选择的温度,它会删除该文本。对于这个例子,我希望给 Kelvin 的数字消失。我试过 "document.getElementById("").style.display = "none"; 但所做的只是删除输入框。我只想让文本消失。有什么办法可以做到吗?

    if(temperature == "Fahrenheit") {

      calculate = Math.round((inputValue - 32) * 0.555);
      document.getElementById("Celsius").value = calculate;

      calculate = Math.round((inputValue - 32) * 0.555 + 273.15);
      document.getElementById("Kelvin").value = calculate;

      // hide fahrenheit value

  }

  else if(temperature == "Celsius") {

    calculate = Math.round((inputValue * 0.555) + 32);
    document.getElementById("Fahrenheit").value = calculate;

    calculate = Math.round(inputValue + 273.15);
    document.getElementById("Kelvin").value = calculate;



    // hide celsius value

  }

我希望你能明白我想问的问题,如果没有,我会尝试详细说明。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    试试:

    document.getElementById('#yourSelector').value=""; /* this will clear the text of the control*/
    

    【讨论】:

      【解决方案2】:

      如何移除输入值

      你的问题有答案:)。为输入字段设置值的方式,使用相同的方式通过设置空字符串来取消设置值。

      document.getElementById("Fahrenheit").value = ""; // set empty string
      

      现在对“摄氏度”做同样的事情。

      如何隐藏输入值

      如果你想隐藏输入值,而不是删除,还有另一种简单的 css 方法可以做到这一点:

      input {
        color: transparent;
      }
      

      或通过javascript:

      document.getElementById("inputId").style.color = "transparent";
      

      快乐编码:)

      【讨论】:

      • 哇,原来这么简单哈哈!非常感谢,我希望我能想到这一点! XD
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2021-09-02
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      相关资源
      最近更新 更多