首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Javascript中使用的HTML中选择特定的用户输入

如何从Javascript中使用的HTML中选择特定的用户输入
EN

Stack Overflow用户
提问于 2020-12-21 02:33:39
回答 1查看 158关注 0票数 1

嗨,我正在尝试建立一个BAC (身体酒精含量)计算器。所有的按钮都是用户的单一输入,除了有3个选择的“酒精类型”。查看下面的HTML。

代码语言:javascript
复制
 <div class="main-container">
        <div class="viewport">
            <div class="gender-buttons" id="gender-buttons">
                <button class="male-button">MALE</button>
                <button class="female-button">FEMALE</button>
            </div>
            <div class="weight-input" >
                <h3>ENTER WEIGHT</h3>
                <input id="weight-input" type="number" placeholder="75kg" required>
            </div>
            <div class="alcohol-content" >
                <h3>I'VE HAD</h3>
                <input id="alcohol-content" type="number" placeholder="5" required>
            </div>
            <div class="alcohol-type">
                <h3>OF</h3>
                <select name="type-of-alcohol" id="alcohol-type">
                    <option value="1.5">Shots of Spirits</option>
                    <option value="5">Glasses of Wine</option>
                    <option value="12">Pints of Beer</option>
                </select>
            </div>
            <div class="submit-button" >
                <input type="submit" id="submit-button">
            </div>
            <div class="result-container" id="result-container">
                
            </div>
        </div>
    </div>

这是我的JS代码。

//投入

代码语言:javascript
复制
   let weightElement = document.getElementById("weight-input");         // User enters weight
   let amountElement = document.getElementById("alcohol-content")       // Number of drinks consumed
   let submitButton = document.getElementById("submit-button")          // Submits calculation 
   const alcoholTypeElement = document.getElementById("alcohol-type")   // Type of alcohol consumed with ounces as measurement of value 

//计算BAC的函数

代码语言:javascript
复制
submitButton.addEventListener('click', function(){               // User clicks Submit button will start the calculation
const weight = parseInt(weightElement.value);                    // Weight will be the integer value of the weightElement input
const amount = parseInt(amountElement.value);                    // Amount will be the integer value of amountElement input 
const alcoholType = parseInt(alcoholTypeElement.value);          // AlcoholType will be the integer value of alcoholTypeElement
const gramsOfAlcohol = (alcoholType*amount)*14;                  // 14 is the standard multipler for US/UK for grams of alcohol/ per standard unit
const genderMultiplyer = 0.55;
const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderMultiplyer))*100

document.getElementById("result-container").innerHTML = 
bloodAlcoholContent.toFixed(2);})

如您所见,所有元素都已链接到各自的JS变量。现在的“酒精型”有三种选择,白酒,葡萄酒和啤酒。每个人都有自己的价值观。我试图将“值”作为条件项合并到我的函数中。

我尝试创建如下所示的switch语句,以反映其酒精体积的值(例如螺旋酒为40%酒精,标准单位为1.5盎司),并在上述变量中(const gramsOfAlcohol =(醇型*量)* 14;其中14是每盎司中找到克酒精的标准乘数:

代码语言:javascript
复制
    const alcoholTypeValue = (alcoholType) => {

    switch(value){
        case 12:
        return value * 0.05                    // 0.05 = 5% Alcohol (Beers)
        break 
        case 5:
        return value * 0.12                    // 0.12 = 5% Alcohol (Wine)
        break 
        case 1.5:
        return value * 0.4                     // 0.40 = 5% Alcohol (Spirits)
        break 

    }

}

因此,如果用户选择烈酒,那么我们就知道,在酒精含量40%乘以14的情况下,标准饮酒量(1.5盎司)会产生8.4克的纯酒精。

因此,如果用户选择葡萄酒,那么我们就知道,以12%的酒精乘以14的标准酒杯(5盎司)将产生8.4克的纯酒精。

因此,如果用户选择啤酒,那么我们就知道,以5%酒精乘以14的标准品脱(12盎司)酒精将产生8.4克的纯酒精。

我希望这是有帮助的,我似乎找不到一个具体的问题,我的问题,但本质上,我最终使用的选择需要预先计算,然后才能用于我的函数。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-21 02:59:29

您实际上并不需要一个函数,一个带有键/值对的简单对象/字典就可以了。另外,您需要在解析酒精类型时使用parseFloat,而不是parseInt,因为它截断1.5到1,从而创建了一个bug。另外,我没有修正你的性别选择,因为我不明白它是如何工作的:

代码语言:javascript
复制
let weightElement = document.getElementById("weight-input"); // User enters weight
let amountElement = document.getElementById("alcohol-content") // Number of drinks consumed
let submitButton = document.getElementById("submit-button") // Submits calculation 
const alcoholTypeElement = document.getElementById("alcohol-type") // Type of alcohol consumed with ounces as measurement of value

submitButton.addEventListener('click', function() { // User clicks Submit button will start the calculation
  const weight = parseInt(weightElement.value); // Weight will be the integer value of the weightElement input
  const amount = parseInt(amountElement.value); // Amount will be the integer value of amountElement input 

  // You had a bug here - need to parseFloat, not parseInt
  const alcoholType = parseFloat(alcoholTypeElement.value); // AlcoholType will be the integer value of alcoholTypeElement

  // This is your object with key/values pairs
  const alcoholTypes = {
    12: 0.05,
    5: 0.12,
    1.5: 0.4
  }

  // here you apply your alcohol type multiplicator alcoholTypes[alcoholType] to amount 
  const gramsOfAlcohol = (alcoholTypes[alcoholType] * amount) * 14; // 14 is the standard multipler for US/UK for grams of alcohol/ per standard unit

  const genderMultiplyer = 0.55;
  const bloodAlcoholContent = (gramsOfAlcohol / ((weight * 1000) * genderMultiplyer)) * 100

  document.getElementById("result-container").innerHTML =
    bloodAlcoholContent.toFixed(2);
})
代码语言:javascript
复制
<div class="main-container">
  <div class="viewport">
    <div class="gender-buttons" id="gender-buttons">
      <button class="male-button">MALE</button>
      <button class="female-button">FEMALE</button>
    </div>
    <div class="weight-input">
      <h3>ENTER WEIGHT</h3>
      <input id="weight-input" type="number" placeholder="75kg" required>
    </div>
    <div class="alcohol-content">
      <h3>I'VE HAD</h3>
      <input id="alcohol-content" type="number" placeholder="5" required>
    </div>
    <div class="alcohol-type">
      <h3>OF</h3>
      <select name="type-of-alcohol" id="alcohol-type">
        <option value="1.5">Shots of Spirits</option>
        <option value="5">Glasses of Wine</option>
        <option value="12">Pints of Beer</option>
      </select>
    </div>
    <div class="submit-button">
      <input type="submit" id="submit-button">
    </div>
    <div class="result-container" id="result-container">

    </div>
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65386887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档