gpt4 book ai didi

javascript - 如何使用javascript获取单选按钮的值

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:51 25 4
gpt4 key购买 nike

我需要使用javascript获取单选按钮的值

我有一个广播组叫做selection

<input type="radio" name="selection" id="selection" value="allClients" checked="checked" />All Clients<br />
<input type="radio" name="selection" id="selection1" value="dateRange" />Date Range between

我使用 javascript 将值传递给另一个页面

onclick="do_action1('liveClickCampaigns','contract_type='+document.getElementById('contract_type').value+'&beginDate='+document.getElementById('beginDate').value+'&endDate='+document.getElementById('endDate').value+'&selection1='+document.getElementById('selection1').value+'&selection='+document.getElementById('selection').value,'content');" type="button">

document.getElementById('selection').value 需要获取值,但即使选择了第二个单选按钮,它也会一直给我第一个单选按钮的值。单选组不在表单内

最佳答案

使用:

function getRadioValue(name) {
var group = document.getElementsByName(name);

for (var i=0;i<group.length;i++) {
if (group[i].checked) {
return group[i].value;
}
}

return '';
}

应用:

var selectedValue = getRadioValue('selection');

演示:

http://www.jsfiddle.net/tqQWT/

关于javascript - 如何使用javascript获取单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3778206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com