开发者

why i dont get the check value in check box?

I have a check box. When one ch开发者_StackOverflow中文版ecks 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.


You should give your checkbox an array-like name attribute: "checkId[]" And the ID attribute should be different for each checkbox, checkId14 or whatever you can track later.


Andr is right and You better not downvote a man for a healthy critique - His answer may not solved Your problem, but html ids should hold unique value, class identifiers can have all the same name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜