Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript获取选中单选按钮的值_Javascript_Jquery_Html_Radio Button - Fatal编程技术网

使用javascript获取选中单选按钮的值

使用javascript获取选中单选按钮的值,javascript,jquery,html,radio-button,Javascript,Jquery,Html,Radio Button,我试图在表单中仅使用JavaScript访问选中单选按钮的值,而不使用jQuery。我查了一下,看到了很多答案,但是当我将相同的代码复制到我的项目中时,由于某种原因它不起作用。这是我的密码 <form> <input type="radio" name="style" value="3" checked> <input type="radio" name="style" value="5"> <input type="rad

我试图在表单中仅使用JavaScript访问选中单选按钮的值,而不使用jQuery。我查了一下,看到了很多答案,但是当我将相同的代码复制到我的项目中时,由于某种原因它不起作用。这是我的密码

<form>
     <input type="radio" name="style" value="3" checked>
     <input type="radio" name="style" value="5">
     <input type="radio" name="style" value="10">
</form>
我假设这会给我3的字符串作为记录的值。当我尝试此示例时,出现以下错误:

request.js:1 Uncaught TypeError: Cannot read property 'value' of null
    at request.js:1
我还尝试使用一个名为style的输入数组的for循环,但也不起作用。它找到了所有三个输入,但在数组中没有它们的值。有人能解释一下怎么了吗?如果这是显而易见的,请耐心等待,我仍在学习,我只是想变得更好。

函数getValue{ var value=document.querySelector'input[name=style]:checked'。值; console.logvalue; } 点击
您的代码是正确的,您只需要一个事件就可以运行选择器代码来获取所选值的值。请参阅下面的代码:-您还可以使用javascript选择器来选择click事件,而不是onclick事件处理程序中的函数位置

函数运行测试{ var radio_ele=document.querySelector'input[name=style]:checked'; console.logradio_ele.value; } 点击
它在一个文件中运行良好复制HTML并将JS插入标记下的同一个文件中您的代码对我有效。你一开始都没有选中单选按钮吗?只有当document.querySelector'input[name=style]:checked'为空时,才会发生错误。只有当A单选按钮尚未在DOM中,或者B单选按钮未被选中时,才会发生错误。我发现了问题。我将JavaScript保存在一个外部文件中。我把它从我的文档头移到了所有HTML下面,这样它就在页面呈现后加载了。我想这就是修复它的方法,对吗?谢谢大家的回复。以前有人因为我问问题而杀了我,但我没有家教,而且我是新来的,所以这就像是该死的感谢,因为我为了找到答案而牺牲了我的精神。
request.js:1 Uncaught TypeError: Cannot read property 'value' of null
    at request.js:1