1

如何在复选框单击事件上使用逗号将值连接到 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);}

上面的代码不起作用。当我单击第一个复选框时,它显示第一个 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(',');
4

3 回答 3

1

警报后您错过了这个:-

document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value= AllOrderIDs
于 2012-08-30T20:33:21.130 回答
1

你错过了那一行:

document.getElementById('ctl00_PagePlaceholder_hdSelectAllOrderId').value = AllOrderIDs;

顺便说一句,不要指代这样的控件。做这个:

document.getElementById('<%=hdSelectAllOrderId.ClientID%>').value = AllOrderIDs;
于 2012-08-30T20:33:47.023 回答
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>
于 2012-08-30T20:52:10.500 回答