简体   繁体   English

如何让我的 Javascript 识别在我的 HTML 中选择了哪个单选按钮选项?

[英]How can I get my Javascript to recognise which radio button option is selected in my HTML?

I'm working on making a small text adventure game over the next week utilising just HTML, CSS, and JavaScript, however I've ran into problems from the get go as I'm trying to have the player be given a selection of choices using radio buttons, so they select a choice, then hit submit, and based on the choice, a number of things could happen.我正在下周仅使用 HTML、CSS 和 JavaScript 制作一个小型文本冒险游戏,但是我从一开始就遇到了问题,因为我试图让玩家有多种选择使用单选按钮,所以他们选择一个选项,然后点击提交,根据选择,可能会发生很多事情。 However I have no idea how to actually achieve this function...但是我不知道如何实际实现这个功能......

I've had a look for tutorials or examples featuring similar questions as mine, however none of them have helped all too much.我已经寻找了与我的问题类似的教程或示例,但是它们都没有太大帮助。

  <form>
    <input type="radio" name="choice" value="A1" checked> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br>
    <input type="radio" name="choice" value="A2"> Proin volutpat eros fringilla felis euismod laoreet a eu velit. <br>
    <input type="radio" name="choice" value="A3"> Mauris orci mi, luctus in leo eget, facilisis imperdiet lacus. <br><br>
    <button type="button" onclick=choiceA()> Choose </button>
  </form>

I'd like to be able to have the used check one of those options, then be able to hit the "Choose" button and continue on their way, having their choice logged in JavaScript for reference later.我希望能够让用户选中这些选项之一,然后能够点击“选择”按钮并继续前进,让他们的选择登录 JavaScript 以供以后参考。

How can I get my Javascript to recognise which radio button option is selected in my HTML?如何让我的 Javascript 识别在我的 HTML 中选择了哪个单选按钮选项?

You can use FormData .您可以使用FormData For that first give an identification to the form.In this example it is done with name attribute.为此,首先给表单一个标识。在这个例子中,它是用name属性完成的。 You can also use id您也可以使用id

Secondly use get method to get the value of selected form其次使用get方法获取所选表单的值

 function choiceA(e) { e.preventDefault(); var form = document.forms.namedItem("fileinfo"); var formData = new FormData(form); console.log(formData.get('choice')) }
 <form name='fileinfo'> <input type="radio" name="choice" value="A1" checked> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br> <input type="radio" name="choice" value="A2"> Proin volutpat eros fringilla felis euismod laoreet a eu velit. <br> <input type="radio" name="choice" value="A3"> Mauris orci mi, luctus in leo eget, facilisis imperdiet lacus. <br><br> <button type="submit" onclick=choiceA(event)> Choose </button> </form>

you can add them inside a div您可以将它们添加到 div 中

<div id="collection">
<label>Value1
    <input name="myradio" type="radio" value="value1" />
</label>
<label>Value2
    <input name="myradio" type="radio" value="value2" />
</label>

and jquery和jQuery

    $(function () {
    $('#collection input[type=radio]').change(function(){
      alert ( $(this).val() ) 
      //do your activity
      })
})

You can use this simple function and get your objective fullfiled.您可以使用这个简单的功能来实现您的目标。 I'm taking the value from radio button whenever the user clicks on any btn, then that value is used to do operation according to the selection in choiceA();每当用户单击任何 btn 时,我都会从单选按钮中获取值,然后该值用于根据 choiceA() 中的选择进行操作;

<form>
    <input type="radio" name="choice" onclick="getRadioValue('A1')" value="A1" checked> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br>
    <input type="radio" name="choice" onclick="getRadioValue('A2')" value="A2"> Proin volutpat eros fringilla felis euismod laoreet a eu velit. <br>
    <input type="radio" name="choice" onclick="getRadioValue('A3')" value="A3"> Mauris orci mi, luctus in leo eget, facilisis imperdiet lacus. <br><br>
<button type="button" onclick=choiceA()> Choose </button>

In js.在js中。 create a function with the same name ie.创建一个同名的函数,即。 getRadioValue(param) getRadioValue(参数)

var globalSelectedValue = 'A1';
function getRadioValue(param){
    globalSelectedValue = param;
    console.log(globalSelectedValue);
}

function choiceA(e){
    doSomething(globalSelectedValue);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的JavaScript无法看到选择了哪个html选项 - Why can't my javascript see which html option is selected 如何在反应中获取输入单选按钮的值? - How can i get the value of my input radio button in react? 我如何 select 用户在我的 javascript 文件夹中选择了哪个单选标记 - How do I select which radio tag the user has selected in my javascript folder 如何确定在Javascript中选择了哪个单选按钮 - How To Determine Which Radio Button Was Selected In Javascript 我如何知道通过 jQuery 选择了哪个单选按钮? - How can I know which radio button is selected via jQuery? 如何在Knockout.js向导中获取单选按钮选项的值? - How do I get the value of radio button option in my Knockout.js wizard? 如何检查我的单选按钮是否已选中并且循环正常工作? - How do I check if my radio button is selected and the loop is working? 如何获得与我的其余javascript一起使用的按钮? - How can I get my button to work with the rest of my javascript? 选择一个后如何隐藏/禁用单选按钮 - How can i hide/disable my radio button once one is selected 如何通过动作脚本在外部HTML内容中获取并设置选定的单选按钮? - How can I get and set the selected radio button in external HTML content through actionscript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM