【问题标题】:How to select a specific user input from HTML to be used in Javascript如何从 HTML 中选择要在 Javascript 中使用的特定用户输入
【发布时间】:2021-03-30 20:52:12
【问题描述】:

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

 <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 代码。

//输入

   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的函数

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 变量。现在“酒精类型”有 3 个选择,烈酒、葡萄酒和啤酒。每个人都有自己的价值观。我正在尝试将“值”作为条件合并到我的函数中。

我尝试创建一个如下所示的 switch 语句来反映其酒精量的值(例如,Spirits 是 40% 酒精,标准单位是 1.5 盎司,并且在上面的变量中(constgramOfAlcohol = (alcoholType*amount )*14;其中 14 是计算每盎司酒精克数的标准乘数:

    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% 的烈酒(1.5 盎司)乘以 14 将得到 8.4 克纯酒精。

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

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

我希望这会有所帮助,我似乎找不到针对我的问题的具体问题,但本质上,我最终使用的选择需要在用于我的函数之前进行预先计算。

谢谢

【问题讨论】:

  • 有什么问题?您描述了您的解决方案,但究竟是什么不起作用?

标签: javascript html if-statement switch-statement


【解决方案1】:

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

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);
})
<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>

【讨论】:

  • @mo-miah,如果对您有帮助,请点赞并接受我的回答:)
  • 您好伊万,感谢您的回答。我看到我必须将“alcoholType”更改为 parseFloat 值才能将其用作整数。但是,查看您的代码 const AlcoholTypes = { 12: 0.05, 5: 0.12, 1.5: 0.4 } 它如何与原始酒精类型相关联?就像用户选择啤酒一样,啤酒的值(5% 酒精,12 盎司)如何使用(5% * 12 盎司)作为计算 BAC 的函数的一部分?
  • 您的 HTML 将根据您的预定义值返回 1.5、5 或 12。 AlcoholTypes 是一个基于键返回某个值的对象。例如,如果您要求密钥 1.5,它将返回 0.05,如果您要求 5,它将返回 0.12。我在下一行中将它用作“alcoholTypes [alcoholType]”,其中酒精类型例如为 1.5,“酒精类型 [1.5]”将为您提供 0.05,您将其乘以“数量”变量。基本上,酒精类型是一种键/值对形式的数据结构,我使用它而不是函数,因为在这里使用函数是一种过度杀伤。
  • 如果在数组中预定义了酒精类型,JS 将如何使用用户输入?如果用户选择“5”杯威士忌,则为 (5*(1.5*14)*0.4),其中“5”是数量,“1.5”是盎司,“14”是克的标准乘数,“ 0.4" 是标准单位中酒精的百分比?这就是为什么我认为最好将它放在一个根据用户输入抛出数据的函数中。想法?
  • 或者它可能像这样工作? 12 是盎司,0.05 是 12 盎司中酒精的百分比,依此类推,所以如果用户选择其中之一,那么它将连接到我想的这些关键值之一?常量酒精类型 = { 12: 12*0.05, 5: 5*0.12, 1.5: 1.5*0.4 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 2011-12-05
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多