[英]Get the value of a radio input button using jQuery
我正在尝试获取已单击的单选按钮的值,并且我正在使用下面的代码。 但我收到以下错误:
jquery-3.5.1.min.js:2 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
我使用的代码:
$(()=> {
$("input:radio").click(()=>{
alert($(this).val())
})
})
问题是您使用的是箭头 function,所以this
是指它在 function 之外引用的任何内容(它不会引用单击的元素)。
$(()=> { $("input:radio").click(function(){ alert($(this).val()); }) })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="demo"> <input type="radio" name="demo" value="a"/> A </label> <label for="demo"> <input type="radio" name="demo" value="b"/> B </label> <label for="demo"> <input type="radio" name="demo" value="c"/> C </label> <label for="demo"> <input type="radio" name="demo" value="d"/> D </label>
错误信息与您提供的代码无关,建议检查Chrome plugin
or other codes
只需使用更简单的内置:checked
参数和val()
function
$('input[name="name_of_your_radiobutton"]:checked').val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.