猿问

如何使用js获取选定的单选按钮值

我正在使用此代码来获取当前所选单选按钮的值,但是它不起作用。


var mailcopy = document.getElementById('mailCopy').value; 

如何使用Javascript获取当前选定的单选按钮值?


肥皂起泡泡
浏览 1541回答 3
3回答

月关宝盒

单选按钮以相同的名称和不同的ID分组,其中之一将被选中的属性设置为true,因此请遍历它们,直到找到它为止。function getCheckedRadio(radio_group) {&nbsp; &nbsp; for (var i = 0; i < radio_group.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var button = radio_group[i];&nbsp; &nbsp; &nbsp; &nbsp; if (button.checked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return button;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return undefined;}var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);if (checkedButton) {&nbsp; &nbsp; alert("The value is " + checkedButton.value);}

芜湖不芜

检查这个<input class="gender" type="radio" name="sex" value="male">Male<br><input class="gender" type="radio" name="sex" value="female">Female<script type="text/javascript">$(document).ready(function () {$(".gender").change(function () {&nbsp; &nbsp; var val = $('.gender:checked').val();&nbsp; &nbsp; alert(val);});});</script>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答