Javascript一次选择所有单选按钮

时间:2013-09-12 06:24:06

标签: javascript php

我遵循php表,其中存在2个单选按钮。一个是现在的学生,另一个是缺席学生。所以现在可以选择一次选择所有单选按钮,但是选择右侧和左侧单选按钮。我只想选择左侧单选按钮,即全部显示。可能是javascript函数不正确。

$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";  
echo "<form method='post' action='$action' name='attendence'/>";

echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";    
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";             
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";             
echo "<td class='tdhead' valign='top' width='200'><b>Present student / absence student</b>
</td>";                 
echo "<td class='tdhead' valign='top' width='200'>
Present All <input type= 'checkbox' onclick='checkAll( this )'</td>";
echo "</tr>";

//start the counter variable
$counter = 1;
while ( $res2 = mysql_fetch_array( $sql2 ) ) {
   $id = (int) $res2['id'];
   $sname = inputvalid( $res2['sname'] );
   $roll = inputvalid( $res2['roll'] );
   $class = inputvalid( $res2['class'] );

   echo "<tr>";
   echo "<td class='tdhead2' valign='top'>$sname</td>";
   echo "<td class='tdhead2' valign='top'>$roll</td>";
   echo "<td class='tdhead2' valign='top'>$class</td>";
   echo "<td class='tdhead2' valign='top'>";

   //echo each radio with the counter for this row
   echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' />&nbsp;";
   echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";

   echo "</td>";
   echo "<td class='tdhead2' valign='top'>&nbsp;</td>";
   echo "</tr>";

   //add one to the counter
   $counter++;
}

echo "<tr>";                
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";             
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit' 
class='submit' /></td>";                
echo "</tr>";               

echo "</table>";
echo "</form>";


if ( isset( $_POST['Submit'] ) && $_POST['Submit'] == "Record" ) {
   if ( isset( $_POST['ch'] ) ) {
       $ch = array_filter($_POST['ch']);
       if ( empty( $ch ) ) {
          echo "<div class='error>Select attendence field. </div>";   
       }
   }

   if ( count( $ch ) == 0 ) {
       echo "<div class='error>Select attendence field. </div>";    
   } else {     
       foreach ( $_POST['ch'] as $roll => $arr1 ) {
       $roll;
       foreach ( $arr1 as $name => $arr2 ) {
           $name;
           foreach ( $arr2 as $class => $value ) {
              $class;
              $value;
              $sql = mysql_query("INSERT INTO e_attendence VALUES('', '$name', '$roll', '$class', '$value', '$current_date')");
           }
       }
    }
}

if ( $sql )         
   echo "<div class='success'>Succesfully recorded.   
</div>";                                    

}   

Javascript代码:

<script language="javascript">
   function checkAll( master ) {
       var checked = master.checked;
       var col = document.getElementsByTagName("INPUT");
       for ( var i = 0; i < col.length; i++ ) {
           col[i].checked= checked;
       }
   }
</script>

1 个答案:

答案 0 :(得分:1)

对于javascript,请使用以下内容: -

first modify the "radio" button code with below:-

echo "<input type='radio' name='ch[$roll][$sname][$class]' class='pres' value='1' />&nbsp;";
echo "<input type='radio' name='ch[$id][$sname][$class]' class='abse' value='0' />";

现在使用javascript 以下所有左侧“广播”

function checkall(el){
var ip = document.getElementsByClassName('pres'), i = ip.length - 1;
for (i; i > -1; --i){
    if(ip[i].type && ip[i].type.toLowerCase() === 'checkbox'){
        ip[i].checked = el.checked;
    }
}
}

并在下面使用所有“右侧无线电”: -

function checkall(el){
var ip = document.getElementsByClassName('abse'), i = ip.length - 1;
for (i; i > -1; --i){
    if(ip[i].type && ip[i].type.toLowerCase() === 'checkbox'){
        ip[i].checked = el.checked;
    }
}
}