页面加载时不会执行正文onload

时间:2013-12-07 10:15:46

标签: javascript ajax

为什么我的身体没有执行?当我加载页面时,我需要显示在drop_1下拉列表中选择的所有记录等于ALL。我有将qp发送到getuser.php的脚本。值发送来自drop_1和tier_two下拉列表。

Combobox.php

        <script type="text/javascript">
$(document).ready(function() {
    $('#wait_1').hide();
    $('#drop_1').change(function(){
            if( $(this).val() == "ALL") {
            $("#wait_1").hide();
            $("#result_1").hide();
        }else{
      $('#wait_1').show();
      $('#result_1').hide();
      $.get("func.php", {
        func: "drop_1",
        drop_var: $('#drop_1').val()
      }, function(response){
        $('#result_1').fadeOut();
        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
      });
      }
        return false;
    });
});

function finishAjax(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>

  <script> // AJAX Implementation
    function showUser() {
        str = document.getElementById("drop_1").value;
        str1 = document.getElementById("tier_two").value;
        if (str == "" || (str != "ALL" && str1 == "")) {
            document.getElementById("txtHint").innerHTML = "";
            return;
        }
        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) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "getuser.php?q=" + str + "&p=" + str1, true);
        xmlhttp.send();
    }
    </script>
    <body>

<?php include('func.php'); ?>
<select name="drop_1" id="drop_1" onchange="showUser(this.value)" style="overflow:scroll;width:100px;">
        <option value="ALL" selected='ALL'>ALL</option>
        <?php getTierOne(); ?>
</select>

    <span id="wait_1" style="display: none;">
    <img alt="Please Wait" src="ajax-loader.gif" width="15px" height="15px"/>
    </span>
    <span id="result_1" style="display: none;"></span>
<div id="txtHint"></div>

    <script>
        showUser();
    </script>

func.php

<?php

function getTierOne()
{
    $mysqli = new mysqli("localhost", "root", "", "app");
    $result = $mysqli->query("SELECT * FROM app GROUP BY app_cn ORDER BY app_cn");
     while($row = $result->fetch_assoc())
        {
           echo '<option value="'.$row['app_cn'].'">'.$row['app_cn'].'</option>';
        }
}

if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
   drop_1($_GET['drop_var']); 
}

function drop_1($drop_var)
{
    $mysqli = new mysqli("localhost", "root", "", "app");
    $results = $mysqli->query("SELECT * FROM app WHERE app_cn='$drop_var' GROUP BY app_plan_no ORDER BY app_plan_no");

    echo '<select name="tier_two" id="tier_two" onchange="showUser()">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

          while($drop_2 = $results->fetch_assoc())
            {
            if($drop_2['app_plan_no'] != '')
            {
              echo '<option value="'.$drop_2['app_plan_no'].'">'.$drop_2['app_plan_no'].'</option>';
            }
            }
    echo '</select> ';
}
?>

Getuser.php

$mysqli = new mysqli("localhost", "root", "", "app");
$p = $_GET['p'];
$q = $_GET['q'];
$where = '';
if ( $q != 'ALL') {
    $where = " WHERE app_cn='$q' AND app_plan_no='$p'  ";
$result1 = $mysqli->query("
    SELECT *
    FROM app 
    $where 
    GROUP BY counter
")or die(mysqli_error());
echo'<table>'
........

3 个答案:

答案 0 :(得分:0)

如果你的意思是在页面完全加载后没有调用showUser()函数,那么在document.ready()回调中调用它。

答案 1 :(得分:0)

更改此部分:

<script>
    showUser();
</script>

到此:

<script>
    function addListener(event, obj, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(event, fn, false);   // modern browsers
        } else {
            obj.attachEvent("on"+event, fn);          // older versions of IE
        }
    }

    addListener('load', window, showUser);
</script>

还要在您的函数中添加一个console.log,以查看它是否被调用。像这样:

function showUser() {
    console.log('Function showUser() was called!!');

然后查看你的萤火虫或者使用浏览器控制台。

答案 2 :(得分:0)

如果您要执行javascript功能,则需要将其添加到<body>标记

<body onLoad="showUser();">

或者您需要在<script type="text/javascript">标记的<head>内实际申请。

window.onload = showUser();

但是目前除了你身体外的随机通话外你都没有。这可能是另一个问题,但您需要将所有相关的展示材料保留在body标记内。