返回JavaScript中的选择选项值

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

Return Select option value in JavaScript

问题

I'd like to return and output to a variable a select option that the user selects in a dropdown menu below.

<h3>Cake Size</h3>
<select id="cakeSize">
    <option value="6">6" Cake</option>
    <option value="8">8" Cake</option>
    <option value="10">10" Cake</option>
    <option value="12">12" Cake</option>
</select>
<button type="button" id="calc">Calculate</button>

I want to get 6/8/10/12 into a variable.

const calc = document.getElementById('calc');
let cakeSize = document.getElementById('cakeSize');
let value = cakeSize.value;

calc.addEventListener('click', function test(){
  return alert(value);
});

Would like to do this initially using a button event listener to trigger the collection of the value.

英文:

I'd like to return and output to a variable a select option that the user selects in a dropdown menu below.

&lt;h3&gt;Cake Size&lt;/h3&gt;
&lt;select id=&quot;cakeSize&quot;&gt;
    &lt;option value=&quot;6&quot;&gt;6&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;8&quot;&gt;8&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;10&quot;&gt;10&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;12&quot;&gt;12&quot; Cake&lt;/option&gt;
&lt;/select&gt;
&lt;button type=&quot;button&quot; id=&quot;calc&quot;&gt;Calculate&lt;/button&gt;

I want to get 6/8/10/12 into a variable.

const calc = document.getElementById(&#39;calc&#39;);
let cakeSize = document.getElementById(&#39;cakeSize&#39;);
let value = cakeSize.value;

calc.addEventListener(&#39;click&#39;, function test(){
  return alert(value);
});

Would like to do this initially using a button event listener to trigger the collection of the value.

答案1

得分: 0

如果您想要“警告”所选的值,请使用alert(cakeSize.value)let value = cakeSize.value;仅具有初始的“select”值。

英文:

If you want to alert the selected value, use alert(cakeSize.value). let value = cakeSize.value; will have only the initial select value.

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

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

let cakeSize = document.getElementById(&#39;cakeSize&#39;);
// let value = cakeSize.value;

calc.addEventListener(&#39;click&#39;, function test() {
  return alert(cakeSize.value);
});

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

&lt;h3&gt;Cake Size&lt;/h3&gt;
&lt;select id=&quot;cakeSize&quot;&gt;
    &lt;option value=&quot;6&quot;&gt;6&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;8&quot;&gt;8&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;10&quot;&gt;10&quot; Cake&lt;/option&gt;
    &lt;option value=&quot;12&quot;&gt;12&quot; Cake&lt;/option&gt;
&lt;/select&gt;
&lt;button type=&quot;button&quot; id=&quot;calc&quot;&gt;Calculate&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 23:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581547.html
匿名

发表评论

匿名网友

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

确定