[英]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.