短脚本不适用于Chrome,但适用于Firefox。

时间:2012-12-03 07:19:34

标签: php javascript

这是一个js脚本复选框,用于选择所有日期。它适用于Firefox但不适用于Chrome。

<script type="text/javascript" charset="utf-8">
function toggle(source) {
      checkboxes = document.getElementsByName('check_time[]');
      for each(var checkbox in checkboxes)
        checkbox.checked = source.checked;
    }
</script>

<?php
session_start();
include ("include/dbconnect.php");
$timestamp = mysql_real_escape_string(strtotime($_POST['date']));
$doc_username = $_SESSION['username'];
$date = @ strip_tags (date("Y-m-d",$timestamp));

$timezone ="Asia/Kuala_Lumpur";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
    $todaydate = date("Y-m-d");

if($date == '')
{
  echo "<i>Please select a date</i>";
}else if ($date < $todaydate)
{   
  echo "<i>* You are not allowed to choose date before $todaydate</i>";

}else
{
$inc   = 1800;
$start = (strtotime('0830')); // 9 AM
$end   = (strtotime('1630')); // 5 PM
echo "<b>*Pick if the time is not available</b><br> <br>";
echo "<form action='doc_confirmtime.php' method='POST'>";
echo "<input type='checkbox' onClick='toggle(this)' />Select All<br/>";
for( $i = $start; $i <= $end; $i += $inc )
{   
    $timeslot = date( 'G:i:s ', $i + $inc );// to the standart format
    $query = mysql_query("SELECT * from appointment Limit 1");

    while($row = mysql_fetch_array($query))
    {
        $date2 = $row['date'];
        $time = $row['time'];

        $query2 = mysql_query("SELECT doc_username, date, time, status from appointment WHERE (doc_username = '$doc_username' AND date = '$date' AND time = '$timeslot') AND (status = '0' OR status = '1' OR status = '2' OR status is NULL)");
        $result = mysql_fetch_array($query2,0);

        if ($result > 0)
        {
            echo "<font size='+1' face='Tahoma, Geneva, sans-serif'><input type='checkbox' name='check_time[]' value='$timeslot' checked = 'checked' DISABLED><b>$timeslot</b></font><br>";
        }else
        {
            echo "<font face='Arial, Helvetica, sans-serif'><input type='checkbox' name='check_time[]' value='$timeslot'>$timeslot</font><br>";
        }


    }
}
echo "<i>*Highlighted Time has chosen by you or your patient</i>";
echo "<br><br>";
echo "<input type='submit' name='submit' value='Submit' style='width:60px; height=30px' />";
echo "<input type='hidden' name='Date' value='$date' >";
echo "</form>";


}
mysql_close($connect);
?>

这实际上是一个由JQuery AJAX html页面运行的php。输入提交按钮后,将显示日期列表(复选框)和“全选”复选框。 Firefox绝对可以使用,但不适用于Chrome。

3 个答案:

答案 0 :(得分:0)

我不确定每个()语法是否适用于其他浏览器而不是Firefox?

您应该使用https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach来迭代元素。

我还建议使用jQuery库,因为当你对自己的语言知之甚少时,它可以简化Javascript的工作。

在jQuery中它看起来像这样:

function toggle(source) {
  $checkboxes = $('input[name=check_time]');
  $checkboxes.each(function(){
    $(this).attr("checked", source.checked);
  });
}

答案 1 :(得分:0)

Chrome仍然不支持您的每个循环。而是使用for

for(var i=0; i<checkboxes.length; i++)
{
    checkboxes[i].checked = source.checked;
}

Chrome目前仅支持Javascript 1.5,您使用的语法是Javascript 1.6(which now looks to be deprecated)的一项功能。这是因为Chrome开发人员的目标是让Chrome与Safari(另一个主要的webkit用户)保持一致。

答案 2 :(得分:-1)

好的,最后我从这个页面得到了回答。在这里,我分享来源

<script language="JavaScript">
function toggle(source) {
   checkboxes = document.getElementsByName('check_time[]');
   for(var i in checkboxes)
      checkboxes[i].checked = source.checked;
   }
</script>