简体   繁体   中英

why i dont get the check value in check box?

I have a check box. When one checks the box, the corresponding ID is placed into a text box. However, when there is only a single value in the database I can't get the check value. Can you tell me why? Here is my code:

<? if(isset($AcceptFriend)) {?>
<form action="<?=site_url()?>friends/Accept_Friend" name="orderform" id="orderform" method="post" style="background:#CCCC99">
<input type="text" name="chId" id="chId" >
<table  border="0" height="50%" id="chkbox" width="50%" >
<tr>
<? foreach($AcceptFriend as $row)
{?>
<tr>
<td>Name</td><td><?=$row['dFrindName'].'</br>';?></td>
<td> <input type="checkbox" name="checkId" id="checkId" value="<? echo  $row['dMemberId']; ?>" onClick="get_check_value()" ></td>
</tr>

<? }}?>
</tr>
 <tr> <td width="10px"><input type="submit" name="submit" id="submit" class="buttn" value="AcceptFriend"></td></tr>

</table>


</form>

This is the script i am using

function get_check_value()
{
    var c_value = "";

    for (var i=0; i < document.orderform.checkId.length; i++)
       {
       if (document.orderform.checkId[i].checked)
          {
          c_value = c_value + document.orderform.checkId[i].value + "\n";
          }
       }
       alert(c_value);
       document.getElementById('chId').value= c_value;
}

If you use an ID once, the returning value of document.orderform.checkId.length will give you the number of properties of that object because document.orderform.checkId will be that single object.

In your loop you will walk thru all these properties and ask them on their property "checked". Because they don't have that property, you will get errors and your script will fail.

When using an ID more than once, document.orderform.checkId will become an array of all objects of that ID and length will return you the number of them.

In that case your loop will walk thru all the checkboxes as you have intended.

The best solution would be to use document.getElementsByName('checkId') or to add a hidden dummy checkbox which will be ignored by starting the loop with i=1 instead of i=0;

Since ID's should be unique, I would recommend to change your scripts to use the name attribute and use document.getElementsByName('checkId') which will always return an array.

您应该为您的复选框提供类似数组的名称属性:“ checkId []”并且每个复选框,checkId14或以后可以跟踪的任何内容的ID属性都应不同。

安德(Andr)是对的,您最好不要对男人进行健康的批评-他的回答可能无法解决您的问题,但是html ID应该具有唯一值,类标识符可以具有相同的名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM