js问题:如何获取checkbox选中值
具体如下:<%!int i = 1;%>
<c:forEach var="s" items="${requestScope.kaoshi.opt}">
<td align="right" valign="top" width="52">
<span class="style5"></span>
<input type="checkbox" name="opt" value="<%=i%>" />
</td>
<td>
${s}
<br>
<hr width="95%" align="left">
</td>
<%
i++;
%>
</tr>
</c:forEach>
然后将其中选中的值加入:
onclick="javascript:window.location='<%=request.getContextPath()%>/exam/next.do?opt=???'"
请问高手们怎么用js实习
答案: <SCRIPT LANGUAGE="JavaScript">
<!--
function getCheckboxValue(){
var opts = document.getElementsByName("opt");
var linkStr = ""; //提交URL,自己改造
for(i=0;i<opts.length;i++){
if(opts[i].checked==true){
linkStr+="&opt="+opts[i].value;
}
}
alert(linkStr);//调试后删除
//javascript:window.location=linkStr;
}
//-->
</SCRIPT>
<!--调试BUTTON-->
<input type="button" value="getCheckboxValue" onclick="getCheckboxValue()"/>你可以这样写
onclick="openWin()"
然后声明一个openWin的函数
<script>
function openWin()
{
var cbo=document.getElementByName("opt");
//循环cbo集合,取得被选中的值
window.location='<%=request.getContextPath()%>/exam/next.do?opt=值
}
<input type="checkbox" id="num1" name="num" value="1"/>
<input type="checkbox" id="num2" name="num" value="2"/>
<input type="button" value="ceshi" onclick="showValue()"/>
<script type="text/javascript">
function showValue(){
var result;
var boxForName = document.getElementsByName("num");
for(var i = 0;i < boxForName.length;i++){
if(boxForName[i].type="checkbox"){
if(boxForName[i].checked){
alert(boxForName[i].value);
}
}
}
}
</script>后面我想你自己应该知道怎么弄吧!
上一个:如何用js获取树节点的中文名字
下一个:怎么执行AJAX 中responseText返回的js呢