请教一个Checkbox 获得选中值Id的问题
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加销售报价</title>
<script type="text/javascript">
//全选
function checkEvent(name,allCheckId){
var allCk=document.getElementById(allCheckId);
if(allCk.checked==true)
checkAll(name);
else
checkAllNo(name);
}
function checkAll(name){
var names=document.getElementsByName(name);
var len=names.length;
if(len>0){
var i=0;
for(i=0;i<len;i++){
names[i].checked=true;
}
}
}
//全不选
function checkAllNo(name){
var names=document.getElementsByName(name);
var len=names.length;
if(len>0){
var i=0;
for(i=0;i<len;i++)
names[i].checked=false;
}
}
//获得选中的值
function chbox(){
var inputs = document.getElementsByName('pName');
for(var i = 0; i<inputs.length; i++){
var obj = inputs[i];
alert(inputs[i].value);
if(obj.type == 'checkbox'){
if(obj.checkbox == true){
alert(obj.value);
}
}
}
}
//返回
function back(){
history.go(-1);
}
</script>
</head>
<body>
<h2 align="center">销售机会---->销售报价管理</h2>
<s:form action="insertSubmit" namespace="/sellorder">
<button onclick="javascript:findByIdFrominsert(document.forms[0],'<c:out value="${filterSalechanceId}"/>');">保存</button>
<button onclick="javascript:findByIdFromlistType(document.forms[0],'<c:out value="${filterSalechanceId}"/>');">添加产品</button>
<button onclick="javascript:findByIdFromlist(document.forms[0],'<c:out value="${filterSalechanceId}"/>');">竞争对手报价</button>
<button onclick="javascript:fromQuote(document.forms[0],'<c:out value="${filterSalechanceId}"/>');">历史报价</button>
<button onclick="back();">返回</button>
<button onclick="chbox()">点击我查看选择哪些</button>
<s:hidden id="filterSalechanceId" name="filterSalechanceId" value=""/>
<s:hidden id="a" name="a" value=""/>
<s:hidden name="filterProductId" id="filterProductId" value=""/>
<s:hidden id="filterOfferDetailedId" name="filterOfferDetailedId" value=""/>
<s:hidden id="filterQuotationId" name="filterQuotationId" value=""/>
<s:hidden id="count" name="count" value=""/>
<s:hidden id="c" name="c" value=""/>
<br/>
<table align="center" border="1px">
<tr>
<td><s:text name="编号"/></td>
<td><s:property value="salechanceId"/> </td>
<td><s:text name="机会来源"/></td>
<td><s:property value="chanceSource"/> </td>
</tr>
<tr>
<td><s:text name="客户名称"/></td>
<td><s:property value="companyName"/> </td>
<td><s:text name="成功几率(%)"/></td>
<td><s:property value="successProbability"/> </td>
</tr>
<tr>
<td><s:text name="概要"/></td>
<td><s:property value="chanceDescribe"/> </td>
<td><s:text name="联系人"/></td>
<td><s:property value="contactName"/> </td>
</tr>
<tr>
<td><s:text name="机会描述"/></td>
<td><s:property value="chanceDescribe"/> </td>
<td><s:text name="联系人电话"/></td>
<td><s:property value="contactTelephone"/> </td>
</tr>
</table>
</s:form>
<!-- 上一次报价 -->
<div align="center">
<form>
<s:token/>
<table align="center">
<tr>
<td colspan="10" align="center">报价单名称:<input type="text" id="filterProductName" name="filterProductName" value="${quotationName}"/></td>
</tr>
<tr>
<td> </td>
<td>全选</td>
<td><input type="checkbox" id="ckall" value="checkbox" onclick="checkEvent('pName','ckall')"/></td>
<td><s:text name="产品名称"/> </td>
<td><s:text name="数量"/> </td>
<td><s:text name="单价(元)"/> </td>
<td><s:text name="成本价(元)"/> </td>
<td><s:text name="核算价(元)"/> </td>
<td><s:text name="周期(元)"/> </td>
<td><s:text name="库存(元)"/> </td>
<td><s:text name="金额(元)"/> </td>
<td>操作</td>
</tr>
<s:if test="list.size()>0">
<s:iterator value="list">
<tr>
<td>
<input type="hidden" id="proName" name="proName" value="${productName }"/>
<input type="hidden" id="proNum" name="proNum" value="${productNum }"/>
<input type="hidden" id="proPrice" name="proPrice" value="${productPrice }"/>
</td>
</tr>
<tr>
<td> <input type="hidden" id="filterOfferDetailedId" name="filterOfferDetailedId" value="<s:property value="offerDetailedId"/>"/></td>
<td> </td>
<td><input type="checkbox" name="pName" value="<s:property value="offerDetailedId" />" id="pName" /></td> <td>${productName} </td>
<td><input type="text" id="productNum" name="productNum" value="${productNum}"/> </td>
<td><input type="text" id="productPrice" name="productPrice" value="${productPrice}"/> </td>
<td>${productNum * productPrice} </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><a href="javascript:fromDelete(document.forms[0],'<s:property value="offerDetailedId"/>');">删除</a></td>
</tr>
</s:iterator>
</s:if>
<s:else>
<tr>
<!--<td colspan="6" align="center"><h4>暂无上次相关报价</h4></td>-->
</tr>
</s:else>
<!-- ============================= 选择产品列表 ============================== -->
<s:iterator value="listp">
<tr>
<td>
<input type="hidden" id="proName" name="proName" value="${productName }"/>
<input type="hidden" id="proNum" name="proNum" value="${productNum }"/>
<input type="hidden" id="proPrice" name="proPrice" value="${productPrice }"/>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="checkbox" name="pName" value="<s:property value="productId"/>" id="pName"/></td>
<td>${productName } </td>
<td><input type="text" id="productNum" name="productNum" value=""/> </td>
<td><input type="text" id="productPrice" name="productPrice" value=""/> </td>
<td><s:property value=""/> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><a href="javascript:fromDelete(document.forms[0],'<s:property value="offerDetailedId"/>');">删除</a></td>
</tr>
</s:iterator>
<tr>
<td><br/></td>
</tr>
<tr>
<td colspan="10" align="right"><button onclick="javascript:findByFrominsert(document.forms[0],'${productId}','${a}');">确定</button></td>
</tr>
</table>
</form>
</div>
</body>
</html>
=============================================================================
如上 。 小弟现在已经能获得到全部的checkbox 值 。但是当选择其中一个的时候还是获得所有的值。
发帖请教一下 。 关键代码已经用红色标识出来 。
[解决办法]
//获得选中的值
function chbox(){
var inputs = document.getElementsByName('pName');
for(var i = 0; i<inputs.length; i++){
var obj = inputs[i];
//alert(inputs[i].value); //这个就不打印了
if(obj.type == 'checkbox'){
if(obj.checked == true){
alert(obj.value);//选中的才打印
}
}
}
}