Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

php - Check all/uncheck all in foreach loop

Please have a look on this code. How can I implement this?

enter image description here

Lately i was done like this -

<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>

But now I need to populate the value from 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; ?> 

How can I implement this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Working jsFiddle Demo

You can't have multiple elements with same ID. You must change your foreach loop. I've deleted the id attribute totally. I'll work with name attribute:

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

As you see in the code, I change the id to the class.

And here is your select all checkbox (I didn't make any changes on it):

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

And your function would be something like this:

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...