输出所选按钮值或名称(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 outputI'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=&
发布评论