除了IE之外,Javascript / AJAX在一切都很好

时间:2012-12-10 14:12:06

标签: php javascript ajax internet-explorer-9

我有一个简单的程序,它可以在Firefox和Chrome中100%运行,但它在Internet Explorer(版本9)中不起作用。

从列出当天所有预定约会的页面调用弹出窗口。这是该弹出窗口的代码:

<?php

$name = $_GET['name'];
#$type = $_GET['type'];
$type = "Regular";
$id   = $_GET['id'];

?>

<!doctype html>
<html>
    <head>
        <title><?php echo $name; ?></title>
        <style type="text/css">
            body {
                font-family: Georgia;
            }
            .col1 {
                float: left;
                padding-right: .75em;
            }
            .col1 div { height: 1.5em; }
            .col2 div { height: 1.5em; }
            .col2 {
                float: left;
                padding-right: .5em;
            }
            .notes {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            function saveClose() {
                var xmlhttp;
                var getstring  = "";
                var d          = document;

                var start_hour = d.getElementsByName('start_hour')[0].value;
                var start_min  = d.getElementsByName('start_min')[0].value;
                var start_am   = d.getElementsByName('start_am')[0].value;

                var end_hour   = d.getElementsByName('end_hour')[0].value;
                var end_min    = d.getElementsByName('end_min')[0].value;
                var end_am     = d.getElementsByName('end_am')[0].value;

                var no_show    = d.getElementsByName('no_show')[0].checked;

                var notes      = d.getElementsByName('notes')[0].value;

                getstring += "?type=<?php echo $type ?>";
                getstring += "&id=<?php echo $id ?>";
                getstring += "&s_hour="+start_hour;
                getstring += "&s_min=" +start_min;
                getstring += "&s_am="  +start_am;
                getstring += "&e_hour="+end_hour;
                getstring += "&e_min=" +end_min;
                getstring += "&e_am="  +end_am;
                getstring += "&ns="    +no_show;
                getstring += "&notes=" +notes;

                if (window.XMLHttpRequest)
                {
                    // code for IE7+, Firefox, Chrome, Opera, Safari;
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5;
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function()
                {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
                        var reply = xmlhttp.responseText;
                        if (reply == "Successful!") {
                            setTimeout('self.close()', 1000);
                        } else {
                            document.getElementById('resultsContent').innerHTML = xmlhttp.responseText;
                        }
                    } else {
                        document.getElementById('resultsContent').innerHTML = "Saving...";
                    }
                }

                xmlhttp.open("GET", "saveClose.php"+getstring, true);
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <div><h2><?php echo $name; ?>'s Consult</h2></div>
        <div class="col1">
            <div>Consult Start Time:</div>
            <div>Consult End Time:</div>
            <div>No Show? :</div>
        </div>
        <div class="col2">
            <div>
                <select name="start_hour">
                    <option></option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                </select>
                :
                <select name="start_min">
                    <option></option>
                    <option>00</option>
                    <option>05</option>
                    <option>10</option>
                    <option>15</option>
                    <option>20</option>
                    <option>25</option>
                    <option>30</option>
                    <option>35</option>
                    <option>40</option>
                    <option>45</option>
                    <option>50</option>
                    <option>55</option>
                </select>
                -
                <select name="start_am">
                    <option></option>
                    <option>AM</option>
                    <option>PM</option>
                </select>
            </div>
            <div>
                <select name="end_hour">
                    <option></option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                </select>
                :
                <select name="end_min">
                    <option></option>
                    <option>00</option>
                    <option>05</option>
                    <option>10</option>
                    <option>15</option>
                    <option>20</option>
                    <option>25</option>
                    <option>30</option>
                    <option>35</option>
                    <option>40</option>
                    <option>45</option>
                    <option>50</option>
                    <option>55</option>
                </select>
                -
                <select name="end_am">
                    <option></option>
                    <option>AM</option>
                    <option>PM</option>
                </select>
            </div>
            <div><input type="checkbox" name="no_show" /></div>
        </div>
        <div class="notes">Notes:</div>
        <div class="notes"><textarea name="notes" rows="5" cols="53"></textarea></div>
        <div class="notes"><button type="button" onClick="saveClose();">Save & Close</button><span id="resultsContent" style="padding-left:1em"></span></div>
    </body>
</html>

AJAX调用运行此PHP脚本:

<?php

$type   = $_GET['type'];
$id     = $_GET['id'];
$s_hour = $_GET['s_hour'];
$s_min  = $_GET['s_min'];
$s_am   = $_GET['s_am'];
$e_hour = $_GET['e_hour'];
$e_min  = $_GET['e_min'];
$e_am   = $_GET['e_am'];
$ns     = $_GET['ns'];
$ns     = (($ns == "true") ? "1" : "0");
$notes  = $_GET['notes'];


switch (true) {
    case empty($type):
        echo "\$type is broken!";
        break;
    case empty($s_hour):
        echo "\$s_hour is broken!";
        break;
    case empty($s_min):
        echo "\$s_min is broken!";
        break;
    case empty($s_am):
        echo "\$s_am is broken!";
        break;
    case empty($e_hour):
        echo "\$e_hour is broken!";
        break;
    case empty($e_min):
        echo "\$e_min is broken!";
        break;
    case empty($e_am):
        echo "\$e_am is broken!";
        break;
    case (!isset($ns)):
        echo "\$ns is broken!";
        break;
    case (!isset($notes)):
        echo "\$notes is broken!";
        break;

    default:
        // SALES APPTS.
        # ...
        # ...
        # ...

        // OTHER APPTS.
        # ...
        # ...
        # ...

        echo "Successful!";
        break;
}


?>

与预期一样,Firefox和Chrome会在每个case语句中暂停,直到选中开始时间和结束时间。效果很好。

然而,无论如何,IE每次都会暂停case empty($s_hour):。我甚至无法通过第一个案例陈述,看看其余的是否也被破坏了。我不明白。

非常感谢任何帮助,因为此应用程序必须与IE9兼容。

2 个答案:

答案 0 :(得分:1)

如果您的值来自下拉菜单,则不应直接使用.value元素上的<select>来访问所选选项(该属性未在所有浏览器上更新)。请改用:

function getDropdownValue(el)
{
    return el.selectedIndex >= 0 ? el.options[el.selectedIndex].value : '';
}

start_hour = getDropdownValue(d.getElementsByName('start_hour')[0]);
// ...

答案 1 :(得分:1)

您正在尝试读取选择框的值的值,但“value”属性可能未按预期填充,因为它试图从所选选项中获取value属性,而不是文本选项。

您应该向<option>元素添加显式值属性,如下所示:

<option value='12'>12</option>

或显式获取所选选项的文本,如下所示:

var objStHr = document.getElementsByName('start_hour')[0];
var start_hour = objStHr.options[objStHr.value];

(为了简洁,我使用了一个temp var来保存对象,因为我们必须引用它两次)

希望有所帮助。