How do I select a value in a list, which stores an object with variables, that uses one of these variables to calculate a sum via user input fields?

huangapple go评论97阅读模式
英文:

How do I select a value in a list, which stores an object with variables, that uses one of these variables to calculate a sum via user input fields?

问题

在HTML列表中,我创建了3个选项,它们都应与一个对象关联。这个对象包含不同的值,我想根据选择在后续函数中计算总和。我一直在尝试找出解决这个JavaScript问题的最简单方法,但我无法弄清楚。是否有解决这个问题的好方法?提前谢谢!

const lxaib = {
  bem: 647,
  bemcg: 2.112,
  crewcg: 2.05,
  paxcg: 2.993,
  bagcg: 3.627,
  fuelcg: 2.413,
}

// 只是一个与应与第一个列表值相关的对象一起工作的示例

function getselectvalue() {
  var selectedvalue = document.getElementById("list").value;
  document.getElementById("test").innerHTML = selectedvalue;
}

function zfm() {
  var selectedvalue = document.getElementById("list").value; // 获取用户选择的值
  var bem = lxaib[selectedvalue]; // 根据用户选择获取对象中存储的变量
  var crew = parseFloat(document.getElementById("crew").value);
  var pax = parseFloat(document.getElementById("pax").value);
  var bag = parseFloat(document.getElementById("baggage").value);
  var zfm = bem + crew + pax + bag;
  document.getElementById("zfm").innerHTML = zfm;
}
<select name="airplanes" id="list" onchange="getselectvalue();">
  <option value="lxaib">LXAIB</option>
  <option value="lxaig">LXAIG</option>
  <option value="lxaif">LXAIF</option>
</select>
<br>
<input type="number" name="crew" id="crew" placeholder="crew">
<br>
<input type="number" name="pax" id="pax" placeholder="pax">
<br>
<input type="number" name="baggage" id="baggage" placeholder="baggage">
<br>
<span id="test"></span>
<br>
<span id="zfm"></span>
<br>
<button onclick="zfm();">Calculate ZFM</button>
英文:

In a HTML list, I have made 3 options, those should be all linked to an object. This object houses different values, from which I want to calculate a sum in a further function, based on the selection. I have been trying to figure out the easiest way to approach this js problem, but I cannot figure it out. Is there any good way to solve this issue? Thanks in advance!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const lxaib = {
  bem: 647,
  bemcg: 2.112,
  crewcg: 2.05,
  paxcg: 2.993,
  bagcg: 3.627,
  fuelcg: 2.413,
}
// Just an example of working with the first object that should be related to the first list value

function getselectvalue() {
  var selectedvalue = document.getElementById(&quot;list&quot;).value;
  document.getElementById(&quot;test&quot;).innerHTML = selectedvalue;
}

function zfm() {
  bem = selectedvalue.bem; // I need to get the variable stored in the object based on the user selection here ... 
  crew = parseFloat(document.getElementById(&quot;crew&quot;).value);
  pax = parseFloat(document.getElementById(&quot;pax&quot;).value);
  bag = parseFloat(document.getElementById(&quot;baggage&quot;).value);
  zfm = bem + crew + pax + bag;
  document.getElementById(&quot;zfm&quot;).innerHTML = zfm;
}

<!-- language: lang-html -->

&lt;select name=&quot;airplanes&quot; id=&quot;list&quot; onchange=&quot;getselectvalue();&quot;&gt;
  &lt;option value=&quot;lxaib&quot;&gt;LXAIB&lt;/option&gt;
  &lt;option value=&quot;lxaig&quot;&gt;LXAIG&lt;/option&gt;
  &lt;option value=&quot;lxaif&quot;&gt;LXAIF&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;crew&quot; id=&quot;crew&quot; placeholder=&quot;crew&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot; pax&quot; id=&quot;pax&quot; placeholder=&quot;pax&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;baggage&quot; id=&quot;baggage&quot; placeholder=&quot;baggage&quot;&gt;
&lt;br&gt;
&lt;span id=&quot;test&quot;&gt;&lt;/span&gt;
&lt;br&gt;
&lt;span id=&quot;zfm&quot;&gt;&lt;/span&gt;
&lt;br&gt;
&lt;button onclick=&quot;zfm();&quot;&gt;Calculate ZFM&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

你可以将所有的输入和按钮包裹在一个表单元素中,然后监听其提交事件以提取所有的值。以下是示例:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const lxaib = {
    bem: 647,
    bemcg: 2.112,
    crewcg: 2.05,
    paxcg: 2.993, 
    bagcg: 3.627, 
    fuelcg: 2.413, 
}

const form = document.querySelector('form');

form.addEventListener('submit', function(e) {
    e.preventDefault();
    var selectedvalue =  document.getElementById("list").value;
    crew = parseFloat(document.getElementById("crew").value);
    pax = parseFloat(document.getElementById("pax").value);
    bag = parseFloat(document.getElementById("baggage").value);
    console.log(selectedvalue, crew, pax, bag);
    zfm = crew + pax + bag; 
    document.getElementById("zfm").innerHTML = zfm; 
});

<!-- language: lang-html -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Code</title>
</head>
<body>
  <form>
    <select name="airplanes" id="list">
        <option value="lxaib">LXAIB</option>
        <option value="lxaig">LXAIG</option>
        <option value="lxaif">LXAIF</option>
    </select>
    <br>
    <input type="number" name="crew" id="crew" placeholder="crew">
    <br>
    <input type="number" name="pax" id="pax" placeholder="pax">
    <br>
    <input type="number" name="baggage" id="baggage" placeholder="baggage">
    <br>
    <span id="zfm"></span>
    <br>
    <button type="submit">Calculate!</button>
  </form>
</body>
</html>

<!-- end snippet -->
英文:

You can wrap all the inputs and the button around a form element and then listen to it's submit event to extract all the values. Here's the example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const lxaib = {
bem: 647,
bemcg: 2.112,
crewcg: 2.05,
paxcg: 2.993, 
bagcg: 3.627, 
fuelcg: 2.413, 
}
const form = document.querySelector(&#39;form&#39;);
form.addEventListener(&#39;submit&#39;, function(e) {
e.preventDefault();
var selectedvalue =  document.getElementById(&quot;list&quot;).value;
crew = parseFloat(document.getElementById(&quot;crew&quot;).value);
pax = parseFloat(document.getElementById(&quot;pax&quot;).value);
bag = parseFloat(document.getElementById(&quot;baggage&quot;).value);
console.log(selectedvalue, crew, pax, bag);
zfm = crew + pax + bag; 
document.getElementById(&quot;zfm&quot;).innerHTML = zfm; 
});

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Test Code&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;select name=&quot;airplanes&quot; id=&quot;list&quot;&gt;
&lt;option value=&quot;lxaib&quot;&gt;LXAIB&lt;/option&gt;
&lt;option value=&quot;lxaig&quot;&gt;LXAIG&lt;/option&gt;
&lt;option value=&quot;lxaif&quot;&gt;LXAIF&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;crew&quot; id=&quot;crew&quot; placeholder=&quot;crew&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot; pax&quot; id= &quot;pax&quot; placeholder=&quot;pax&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;baggage&quot; id=&quot;baggage&quot; placeholder=&quot;baggage&quot;&gt;
&lt;br&gt;
&lt;span id=&quot;zfm&quot;&gt;&lt;/span&gt;
&lt;br&gt;
&lt;button type=&quot;submit&quot;&gt;Calculate!&lt;/button&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 1

您的数据对象需要包含与选择匹配的键的对象中。

然后,您可以使用方括号表示法获取对象。

selected = data[selectedvalue];

此外,所选变量需要在函数外部声明,以便在其他函数中访问它。

最好使用事件侦听器而不是内联事件处理程序(如 onclick、onchange 等)。

const data = {
  "lxaib": {
    bem: 647,
    bemcg: 2.112,
    crewcg: 2.05,
    paxcg: 2.993,
    bagcg: 3.627,
    fuelcg: 2.413,
  },
  "lxaig": {
    bem: 647,
    bemcg: 2.112,
    crewcg: 2.05,
    paxcg: 2.993,
    bagcg: 3.627,
    fuelcg: 2.413,
  },
  "lxaif": {
    bem: 647,
    bemcg: 2.112,
    crewcg: 2.05,
    paxcg: 2.993,
    bagcg: 3.627,
    fuelcg: 2.413,
  }
}

let selected;

function getselectvalue() {
  const selectedvalue = document.getElementById("list").value;
  selected = data[selectedvalue];
  document.getElementById("test").innerHTML = selectedvalue;
}

function zfm() {
  const bem = selected.bem; // 我需要根据用户在此处选择的变量来获取对象中存储的变量...
  const crew = parseFloat(document.getElementById("crew").value);
  const pax = parseFloat(document.getElementById("pax").value);
  const bag = parseFloat(document.getElementById("baggage").value);
  const zfm = bem + crew + pax + bag;
  document.getElementById("zfm").innerHTML = zfm;
}

getselectvalue();

const calc = document.querySelector(".calc");
const planes = document.querySelector("#list");

calc.addEventListener("click", zfm);
planes.addEventListener("change", getselectvalue);

希望这对您有所帮助。

英文:

Your data object will need to be in an object with the keys matching that of the select.

Then you get the object via bracket notation.

selected = data[selectedvalue];

Also, the selected variable will need to be declared outside of the function so that it can be access within the other functions.

Also it is best to use event listeners not inline event handlers (like onclick, onchange etc)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = {
&quot;lxaib&quot;: {
bem: 647,
bemcg: 2.112,
crewcg: 2.05,
paxcg: 2.993,
bagcg: 3.627,
fuelcg: 2.413,
},
&quot;lxaig&quot;: {
bem: 647,
bemcg: 2.112,
crewcg: 2.05,
paxcg: 2.993,
bagcg: 3.627,
fuelcg: 2.413,
},
&quot;lxaif&quot;: {
bem: 647,
bemcg: 2.112,
crewcg: 2.05,
paxcg: 2.993,
bagcg: 3.627,
fuelcg: 2.413,
}
}
let selected;
function getselectvalue() {
const selectedvalue = document.getElementById(&quot;list&quot;).value;
selected = data[selectedvalue];
document.getElementById(&quot;test&quot;).innerHTML = selectedvalue;
}
function zfm() {
const bem = selected.bem; // I need to get the variable stored in the object based on the user selection here ... 
const crew = parseFloat(document.getElementById(&quot;crew&quot;).value);
const pax = parseFloat(document.getElementById(&quot;pax&quot;).value);
const bag = parseFloat(document.getElementById(&quot;baggage&quot;).value);
const zfm = bem + crew + pax + bag;
document.getElementById(&quot;zfm&quot;).innerHTML = zfm;
}
getselectvalue();
const calc = document.querySelector(&quot;.calc&quot;);
const planes = document.querySelector(&quot;#list&quot;);
calc.addEventListener(&quot;click&quot;,zfm);
planes.addEventListener(&quot;change&quot;,getselectvalue);

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Test Code&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;select name=&quot;airplanes&quot; id=&quot;list&quot;&gt;
&lt;option value=&quot;lxaib&quot;&gt;LXAIB&lt;/option&gt;
&lt;option value=&quot;lxaig&quot;&gt;LXAIG&lt;/option&gt;
&lt;option value=&quot;lxaif&quot;&gt;LXAIF&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;crew&quot; id=&quot;crew&quot; placeholder=&quot;crew&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot; pax&quot; id=&quot;pax&quot; placeholder=&quot;pax&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;number&quot; name=&quot;baggage&quot; id=&quot;baggage&quot; placeholder=&quot;baggage&quot;&gt;
&lt;br&gt;
&lt;span id=&quot;test&quot;&gt;&lt;/span&gt;
&lt;br&gt;
&lt;span id=&quot;zfm&quot;&gt;&lt;/span&gt;
&lt;br&gt;
&lt;button class=&quot;calc&quot;&gt;Calculate ZFM&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案3

得分: 1

我的建议:对在线文档产生兴趣,链接如下:
https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
const
  myForm = document.querySelector('#my-form')
, airplanes_val = 
  { lxaig: { bem: 100 }
  , lxaif: { bem: 300 }
  , lxaib: 
    { bem    : 647
    , bemcg  : 2.112
    , crewcg : 2.05
    , paxcg  : 2.993
    , bagcg  : 3.627
    , fuelcg : 2.413
    } 
  };
myForm.onsubmit = e =>
  {
  e.preventDefault(); // 禁止表单提交

  myForm.respons.textContent 
    = airplanes_val[myForm.airplanes.value].bem
    + myForm.crew   .valueAsNumber    // 使用 .valueAsNumber 属性
    + myForm.pax    .valueAsNumber
    + myForm.baggage.valueAsNumber
    ;
  }

<!-- 语言: lang-css -->
#my-form output { color: red; }

<!-- 语言: lang-html -->
<form id="my-form">
  <select name="airplanes">   <!-- 使用表单元素无需使用 id -->
    <option value="lxaib">LXAIB</option>
    <option value="lxaig">LXAIG</option>
    <option value="lxaif">LXAIF</option>
  </select>
  <br> <input type="number" name="crew"    placeholder="crew">
  <br> <input type="number" name="pax"     placeholder="pax">
  <br> <input type="number" name="baggage" placeholder="baggage">
  <br> <output              name="respons">...</output>
  <br> <button>Calculate ZFM</button>
</form>

<!-- 结束代码片段 -->

这是您提供的代码的翻译部分。

英文:

my advice: take an interest in the online documentation
https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
myForm = document.querySelector(&#39;#my-form&#39;)
, airplanes_val = 
{ lxaig: { bem: 100 }
, lxaif: { bem: 300 }
, lxaib: 
{ bem    : 647
, bemcg  : 2.112
, crewcg : 2.05
, paxcg  : 2.993
, bagcg  : 3.627
, fuelcg : 2.413
} 
};
myForm.onsubmit = e =&gt;
{
e.preventDefault(); // disable form submiting
myForm.respons.textContent 
= airplanes_val[myForm.airplanes.value].bem
+ myForm.crew   .valueAsNumber    // use  .valueAsNumber property
+ myForm.pax    .valueAsNumber
+ myForm.baggage.valueAsNumber
;
}

<!-- language: lang-css -->

#my-form output { color: red; }

<!-- language: lang-html -->

&lt;form id=&quot;my-form&quot;&gt;
&lt;select name=&quot;airplanes&quot;&gt;   &lt;!-- with a form element no needs to use id--&gt;
&lt;option value=&quot;lxaib&quot;&gt;LXAIB&lt;/option&gt;
&lt;option value=&quot;lxaig&quot;&gt;LXAIG&lt;/option&gt;
&lt;option value=&quot;lxaif&quot;&gt;LXAIF&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt; &lt;input type=&quot;number&quot; name=&quot;crew&quot;    placeholder=&quot;crew&quot;&gt;
&lt;br&gt; &lt;input type=&quot;number&quot; name=&quot;pax&quot;     placeholder=&quot;pax&quot;&gt;
&lt;br&gt; &lt;input type=&quot;number&quot; name=&quot;baggage&quot; placeholder=&quot;baggage&quot;&gt;
&lt;br&gt; &lt;output              name=&quot;respons&quot;&gt;...&lt;/output&gt;
&lt;br&gt; &lt;button&gt;Calculate ZFM&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月27日 22:35:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定