-1

请查看此代码。我该如何实施?

在此处输入图像描述

最近我是这样做的 -

<script type="text/javascript">
function CheckPowerAll() {
    if (document.getElementById("PO_ALL").checked == true) {

        document.getElementById("PO_PowerSteering").checked = true;
        document.getElementById("PO_PowerMirrors").checked = true;
    } else {

        document.getElementById("PO_PowerSteering").checked = false;
        document.getElementById("PO_PowerMirrors").checked = false;
    }
}
</script>

<tr>
  <td><input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
Select all <span class="bold">Power Options</span> </td>
   </tr>
 <tr>
<td><table width="85%" border="0" cellspacing="0" cellpadding="0">
 <tr>
<td class="box2">

   <input name="PO_PowerSteering" type="checkbox" id="PO_PowerSteering" value="Power Steering" />
Power Steering<br />
   <input name="PO_PowerMirrors" type="checkbox" id="PO_PowerMirrors" value="Power Mirrors" />
Power Mirrors <br /></td>
  </tr>
</table></td>
  </tr>
<tr>

但现在我需要从 DB 中填充值。

<input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
Select all <span class="bold">Power Options</span> </td>
   </tr>
 <tr>
<td><table width="85%" border="0" cellspacing="0" cellpadding="0">
 <tr>
<td class="box2">

<?php 

$query = mysql_query("SELECT * FROM vehicle_poweroptions"); 
    while ( $results[] = mysql_fetch_object ($query));
      array_pop ( $results );
        foreach ( $results as $option ) : ?>

<input name="PO_PowerWindows" type="checkbox" id="PO_PowerWindows" value="<?php echo  $option->id; ?>" />

<?php echo  $option->type; ?><br />

<?php endforeach; ?> 

我该如何实施?

4

3 回答 3

3

工作 jsFiddle 演示

您不能有多个具有相同 ID 的元素。你必须改变你的foreach循环。我已经id完全删除了该属性。我将使用name属性:

<?php foreach ( $results as $option ) : ?>
    <input
        name="PO_PowerWindows"
        type="checkbox"
        value="<?php echo  $option->id; ?>"
    />
<?php endforeach; ?> 

正如您在代码中看到的,我idclass.

这是您的全选复选框(我没有对其进行任何更改):

<input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />

你的功能将是这样的:

function CheckPowerAll() {
    var elements = document.getElementsByName("PO_PowerWindows");
    var l = elements.length;

    if (document.getElementById("PO_ALL").checked) {
        for (var i = 0; i < l; i++) {
            elements[i].checked = true;
        }
    } else {
        for (var i = 0; i < l; i++) {
            elements[i].checked = false;
        }
    }
}
于 2013-05-30T05:47:19.470 回答
1
<input name="PO_PowerWindows[]" type="checkbox" id="PO_PowerWindows" value="<?php echo  $option->id; ?>" checked="checked"/>
于 2013-05-30T05:21:46.410 回答
1

只需添加

array_pop ( $results ); ?>
  <input name="PO_ALL" type="checkbox" id="PO_ALL" value="checkbox" onclick="CheckPowerAll()" />
<?php    foreach ( $results as $option ) : ?>
于 2013-05-30T05:22:41.833 回答