如何在输入类型号上显示自动计算值?

JCFP

我刚开始在 Scrimba 中学习 JavaScript,并且有一个练习部分可以进行单位转换,但我已经做了静态转换,然后我挑战自己添加用户输入,但我只是精疲力尽我现在很困惑

这是我的html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <main>
        <div class="unit">
            <h2 id="heading">Metric/Imperial Unit Converstion</h2>
            <!-- <h2 id="numToConvert"> 0 </h2> -->
            <input type="number" value="num" id="inputNum" min="0" onchange="unitConvert()">
            
        </div>
    </main>
    <section> 
        <div class="main-results"> 
            <div class="conversion-results">
                <p>Length (Meter/Feet)
                <p id="meterFeet">0 meters = 0.000 feet | 0 feet = 0.000 meters</p>
            </div>
            <div class="conversion-results">
                <p>Volumes (Liters/Gallons)
                <p id="litersGallons">0 liters = 0.000 gallons | 0 gallons = 0.000 liters</p>
            </div>
            <div class="conversion-results">
                <p>Mass (Kilograns/Pounds)
                <p id="kiloPounds">0 kilos = 0.000 pounds | 0 pounds = 0.000 kilos</p>
            </div>
        </div>
    </section>
    <script src="script.js"></script>
</body>
</html>

这是我的 JavaScript 代码:

const unit = document.getElementById("inputNum").value;

function unitConvert() {
    const meterToFeet = (unit * 3.28084).toFixed(3)
    const feetToMeter = (unit * 0.3048).toFixed(3)

    const litersToGallons = (unit * 0.264172).toFixed(3)
    const GallonsToLiters = (unit * 3.785412).toFixed(3)

    const kilogramsToPounds = (unit * 2.2).toFixed(3)
    const poundsToKilograms = (unit / 2.2).toFixed(3)

    //Meter to Feet/Vice Versa
    document.getElementById("meterFeet").textContent = unit +" meters = "+ meterToFeet +" feet | "+unit+" feet = "+feetToMeter+" meters"
    //Liters to Gallons/Vice Versa
    document.getElementById("litersGallons").textContent = unit +" liters = "+ litersToGallons +" gallons | "+unit+" gallons = "+GallonsToLiters+" liters"
    //Kilo to Pounds/Vice Versa
    document.getElementById("kiloPounds").textContent = unit +" kilos = "+ kilogramsToPounds +" pounds | "+unit+" pounds = "+poundsToKilograms+" kilos"
}

我的瓶子 O Scrumpy

你可以试试这个代码我希望这是你要求的

function unitConvert() {


    const unit = document.getElementById("inputNum").value;
    const meterToFeet = (unit * 3.28084).toFixed(3)
    const feetToMeter = (unit * 0.3048).toFixed(3)

    const litersToGallons = (unit * 0.264172).toFixed(3)
    const GallonsToLiters = (unit * 3.785412).toFixed(3)

    const kilogramsToPounds = (unit * 2.2).toFixed(3)
    const poundsToKilograms = (unit / 2.2).toFixed(3)

    //Meter to Feet/Vice Versa
    document.getElementById("meterFeet").textContent = unit +" meters = "+ meterToFeet +" feet | "+unit+" feet = "+feetToMeter+" meters"
    //Liters to Gallons/Vice Versa
    document.getElementById("litersGallons").textContent = unit +" liters = "+ litersToGallons +" gallons | "+unit+" gallons = "+GallonsToLiters+" liters"
    //Kilo to Pounds/Vice Versa
    document.getElementById("kiloPounds").textContent = unit +" kilos = "+ kilogramsToPounds +" pounds | "+unit+" pounds = "+poundsToKilograms+" kilos"
}

const inputField = document.querySelector('#inputNum');
inputField .addEventListener('input', function () {
  unitConvert()
});
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  color: #fcbe24;
  background-color: #18222d;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <main>
        <div class="unit">
            <h2 id="heading">Metric/Imperial Unit Converstion</h2>
            <!-- <h2 id="numToConvert"> 0 </h2> -->
            <input type="number" value="num" id="inputNum" min="0" onchange="unitConvert()">
            
        </div>
    </main>
    <section> 
        <div class="main-results"> 
            <div class="conversion-results">
                <p>Length (Meter/Feet)
                <p id="meterFeet">0 meters = 0.000 feet | 0 feet = 0.000 meters</p>
            </div>
            <div class="conversion-results">
                <p>Volumes (Liters/Gallons)
                <p id="litersGallons">0 liters = 0.000 gallons | 0 gallons = 0.000 liters</p>
            </div>
            <div class="conversion-results">
                <p>Mass (Kilograns/Pounds)
                <p id="kiloPounds">0 kilos = 0.000 pounds | 0 pounds = 0.000 kilos</p>
            </div>
        </div>
    </section>
    <script src="script.js"></script>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从输入类型号中自动删除逗号

如何在输入类型号中添加整数?

如何在输入类型号时禁用“onchange()”

将输入类型号的最小值设置为其他输入类型号的值

如何从输入类型号和数据库(获取)值中减去一个值?减去后,在更新查询上更新当前余额

如何在iOS上获取设备的型号和型号?

如何在引导程序中使用输入类型=密码上的工具提示显示输入的密码?

如何在接受浮点值的输入类型上设置最小值和最大值?

如何在Material ui自动完成的输入文本字段中显示所选值?

在Vue组件上编辑数据时如何显示输入类型日期的值?

如何获取输入类型=图像的值并将其显示在图像上?

如何在值输入文本上显示选择选项的值?(vue.js 2)

榆木如何基于输入类型号更新模型

如何在输入字段中显示值

如何在自动完成组件中的焦点和模糊上显示不同的值

如何在vue.js 2上更新输入类型文本中的值?

如何在redux表单日期输入字段上显示初始值

如何在tomcat上的servlet程序中以表单形式显示用户输入的值?

根据物料类型自动计算输入日期

如何在禁用自动计算的ValueAxis上设置边距?

如何在DOS上的BATch文件中自动输入?

如何在Chrome中的输入字段上禁用自动对焦?

如何在文本输入字段上设置自动宽度/长度?

如何在Windows bat脚本中输入自动值(无)?

如何在Xamarin Forms上获得当前的设备型号?

如何在 Ubuntu 20.04 上识别 AMD 显卡型号

如何在 HTML 输入类型“时间”中显示当前时间?

如何在选择输入中计算相同值的数量

如何在Ubuntu One上使用Keepass自动类型