获取 checkbox 和 radio 选定的值,以及设默认值

时间:2022-10-08 18:58:27


上述代码 实现了,获取 checkbox  radio 选定的值,以及根据值 显示表单默认已经选择的值<p></p>window.οnlοad=function(){
var condition = document.getElementById("conditiontext").value;
var radioObject = document.getElementsByName("condition");
if(condition === "")
{
radioObject[0].checked = true;
}
for (var i = 0; i < radioObject.length; i++)
{
if(radioObject[i].value == condition)
{
radioObject[i].checked = true;
break;
}

}
var groupcodes = document.getElementById("groupcodetext").value;
var checkboxObject=document.getElementsByName("groupcode");
if(groupcode === "")
{
checkboxObject[0].checked = true;
}
for (var i = 0; i < radioObject.length; i++)
{
if(groupcodes.indexOf(checkboxObject[i].value)!=-1)
{
checkboxObject[i].checked = true;
}

}
}
function goPage(id) {
var pageNum = document.getElementById("pageNum").value;
var condition = document.getElementById("conditiontext").value;
var groupcodes = document.getElementById("groupcodetext").value;
location.href="url?id="+id+"&pageNum=" + pageNum+"&groupcode=" + groupcodes+"&condition="+condition;
}
function onChangeGroupcode(id) {
var checkboxVal=document.getElementsByName("groupcode");
var str="";
for(var i=0;i<checkboxVal.length;i++)
{
if(checkboxVal[i].checked==true){
str+="'"+checkboxVal[i].value+"',";
}
}
if(str.endsWith(","))
{
str = str.substring(0,str.length-1);
}
var condition = document.getElementById("conditiontext").value;
location.href="<span style="font-family: Arial, Helvetica, sans-serif;">url</span><span style="font-family: Arial, Helvetica, sans-serif;">?id="+id+"&groupcode=" + str+"&condition="+condition;</span>
}
function onChangeCondition(id) {
var condition = "";
var checkboxVal=document.getElementsByName("condition");
for(var i=0;i<checkboxVal.length;i++)
{
if(checkboxVal[i].checked==true){
condition=checkboxVal[i].value;
break;
}
}
if(condition=="")
{
condition="AllPartner";
}
var groupcode = document.getElementById("groupcodetext").value;
location.href="url?id="+id+"&groupcode=" + groupcode+"&condition="+condition;
}


</pre><pre name="code" class="html">
</pre><pre name="code" class="html"><tr><td>  用户所在的组:<input type='checkbox' name="groupcode" id="groupcode" οnchange="onChangeGroupcode('<%=id%>')" value="Partner" checked disabled/>A
<input type='checkbox' name="groupcode" id="groupcode" οnchange="onChangeGroupcode('<%=id%>')" value="Employee"/>B
<input type='checkbox' name="groupcode" id="groupcode" οnchange="onChangeGroupcode('<%=id%>')" value="Member"/>C
<input type="hidden" name="groupcodetext" id="groupcodetext" value="<%=groupcodes %>" />
</td>
<td align="right">
<input type='radio' name="condition" id="condition" οnchange='onChangeCondition("<%=id%>")' value="AllPartner" checked/>全部User
<input type='radio' name="condition" id="condition" οnchange='onChangeCondition("<%=id%>")' value="UnusedPartner"/>未使用User
<input type='radio' name="condition" id="condition" οnchange='onChangeCondition("<%=id%>")' value="UsedPartner"/>已使用User
<input type="hidden" name="conditiontext" id="conditiontext" value="<%=condition %>" />
</td></tr>





上述代码 实现了,获取 checkbox 和 radio 选定的值,以及根据值 显示表单默认已经选择的值