输出所选按钮值或名称(Output selected button value or name)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
输出所选按钮值或名称(Output selected button value or name)

我正在尝试从按钮中获取值并在窗口中输出已选择的内容,因为我说数组未定义

请帮助纠正我的错误。

<input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked" />Vanilla <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate <input type="radio" name="flavor" id="strawberry" value="Strawberry" />Strawberry var flavorArray = ["","","Vanilla","Chocolate","Strawberry"]; var flavorValue = document.querySelector('input[name = "flavor"]:checked').value; flavorArray[flavorvalue] -<< my output

I'm trying to get values from the button and output in the window what has been selected, for me says array is undefined.

Please help correct my mistake.

<input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked" />Vanilla <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate <input type="radio" name="flavor" id="strawberry" value="Strawberry" />Strawberry var flavorArray = ["","","Vanilla","Chocolate","Strawberry"]; var flavorValue = document.querySelector('input[name = "flavor"]:checked').value; flavorArray[flavorvalue] -<< my output

最满意答案

试试这段代码:

<html> <head> <title>Checkbox</title> </head> <body> <input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked"/>Vanilla <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate <input type="radio" name="flavor" id="strawberry" value="Strawberry"/>Strawberry <script> var flavorArray = ["","","Vanilla","Chocolate","Strawberry"]; var flavorValue = document.querySelector('input[name="flavor"]:checked').value; console.log(flavorArray[flavorArray.indexOf(flavorValue)]) </script> </body> </html>

Try this code:

<html> <head> <title>Checkbox</title> </head> <body> <input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked"/>Vanilla <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate <input type="radio" name="flavor" id="strawberry" value="Strawberry"/>Strawberry <script> var flavorArray = ["","","Vanilla","Chocolate","Strawberry"]; var flavorValue = document.querySelector('input[name="flavor"]:checked').value; console.log(flavorArray[flavorArray.indexOf(flavorValue)]) </script> </body> </html>

更多推荐

value,flavor,<input,type,radio,电脑培训,计算机培训,IT培训"/> <meta name=&

本文发布于:2023-04-24 21:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/aa10eb6ae28b2ed815db91ffa81dc914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所选   按钮   名称   button   selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!