如何使用复选框单击事件上的逗号将值连接到hdnfield值。当我单击复选框时,我选择顺序并传递给JS函数。如果我选择三个复选框,那么hdnfield值应该像1,2,3 ......就像那样...怎么做?
if (chkBoxOne != null)
{
chkBoxOne.Attributes.Add("onclick", "javascript:return SelectOne('" + chkBoxOne.ClientID + "','" + e.Row.ClientID + "','" + lblorderId.Text + "')");
//if (chkBoxOne.Checked)
// hdSelectAllOrderId.Value += ((Label)e.Row.FindControl("lblorderId")).Text + ",";
}
function SelectOne(id, rowID, OrderID) {
var AllOrderIDs = 0;
AllOrderIDs = Number(document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value);
alert(AllOrderIDs);
if (document.getElementById(id).checked == true) {
if (AllOrderIDs == '')
AllOrderIDs = OrderID;
else
AllOrderIDs = AllOrderIDs + ' ,' + OrderID;
}
alert(AllOrderIDs);}
以上代码无效。当我点击第一个复选框时它显示frist ordid,但是当我点击第二个时它没有显示第一个ordid,这是我已经分配给它。它只显示第二个...
var AllOrderIDs = 0;
AllOrderIDs = document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value;
var IDs = AllOrderIDs.split(',');
if (document.getElementById(id).checked == true) {
if (IDs.indexOf(OrderID) == -1) {
IDs.push(OrderID);
}
}
else {
var index = IDs.indexOf(OrderID);
if (index != -1) {
IDs = IDs.slice(index, 1);
}
}
AllOrderIDs = IDs.join(',');
答案 0 :(得分:1)
警告后你错过了这个: -
document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value= AllOrderIDs
答案 1 :(得分:1)
你错过了那条线:
document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value = AllOrderIDs;
顺便说一下,不要参考这样的控件。这样做:
document.getElementById('<%=hdSelectAllOrderId.ClientID%>').value = AllOrderIDs;
答案 2 :(得分:0)
首先,是的,您忘记将值保存到隐藏字段。 其次,当复选框状态更改为“已选中”时,您只处理该情况。因此,您不会从隐藏字段中删除ID。如果再次选中 - 取消选中并再次检查,则会保存重复的ID。 我建议您将字符串拆分为ID数组,然后使用它。
<html>
<head>
<script type="text/javascript">
function Select(sender, OrderID)
{
var AllOrderIDs = document.getElementById('hf1').value;
var IDs = AllOrderIDs.split(',');
if (sender.checked == true)
{
if (IDs.indexOf(OrderID.toString()) == -1)
{
IDs.push(OrderID);
}
}
else
{
var index = IDs.indexOf(OrderID.toString());
if (index != -1)
{
IDs.splice(index, 1);
}
}
AllOrderIDs = IDs.join(',');
alert(AllOrderIDs);
document.getElementById('hf1').value = AllOrderIDs;
}
function Check()
{
var AllOrderIDs = document.getElementById('hf1').value;
alert(AllOrderIDs);
}
</script>
</head>
<body>
<input type="hidden" id="hf1" />
<input type="checkbox" id="check1" onclick="Select(this, 1);" />
<input type="checkbox" id="check2" onclick="Select(this, 2);" />
<input type="checkbox" id="check3" onclick="Select(this, 3);" />
<br />
<p> <input type="button" onclick="Check();" value="Check" /> </p>
</body>
</html>