如何在页面加载

时间:2016-02-09 05:30:32

标签: javascript php jquery

在我的php页面中我有四个下拉列表,我想要选择第一个和第二个下拉列表,这些来自数据库,但我在选择第二个下拉列表时遇到问题。当我更改第一个下拉列表的值时,它正在工作,它正在进行值更改,但是当页面加载和我的函数是$ .streamSelection时,它不会工作。

<script>
    $(document).ready(function() {
        $('#<?php echo $stream;?>').prop('selected', true);
        $('.sidebar-menu .system').addClass('active');
        $('.sidebar-menu .system .student').addClass('active');
        $(".sidebar-menu .system").tree();
        $('#streams').change(function() {
            $('#branches').html("<option value=''>-- Select --</option>");
            $('#batches').html("<option value=''>-- Select --</option>");
            $('.divAfter').hide();
            $('.divBefore').show();
            $('.semesterAfter').hide();
            $('.semesterBefore').show();
            $('.btnAdd').hide();
            $('.btnExcel').hide();
            $('.studList').hide();
            if ($(this).val() == '') {} else {
                $.when($.streamSelection($(this).val())).then($.listSemester($(this).val()));
            }
        });
        $('#branches').change(function() {
            $('#batches').html("<option value=''>-- Select --</option>");
            $('.divAfter').hide();
            $('.divBefore').show();
            $('.semesterAfter').hide();
            $('.semesterBefore').show();
            $('.btnAdd').hide();
            $('.btnExcel').hide();
            $('.studList').hide();
            if ($(this).val() == '') {} else {
                $.branchSelection($(this).val());
            }
        });
        $('#batches').change(function() {
            if ($(this).val() == '') {
                $('.divAfter').hide();
                $('.divBefore').show();
                $('.semesterAfter').hide();
                $('.semesterBefore').show();
                $('.btnAdd').hide();
                $('.btnExcel').hide();
                $('.studList').hide();
            } else {
                $('.divBefore').hide();
                $('#division').val('');
                $('.divAfter').show();
            }
        });
        $('#division').change(function() {
            $('.studList').hide();
            $('.semesterAfter').hide();
            $('.semesterBefore').show();
            $('.btnAdd').hide();
            $('.btnExcel').hide();
            if ($(this).val() != '') {
                $('.semesterBefore').hide();
                $('#sem').val('');
                $('.semesterAfter').show();
            }
        });
        $('#sem').change(function() {
            $('.btnAdd').hide();
            $('.btnExcel').hide();
            $('.studList').hide();
            if ($(this).val() != '') {
                $.when($('.btnAdd').attr('href', 'add.php?streamId=' + $('#streams').val() + '&branchId=' + $('#branches').val() + '&batchId=' + $('#batches').val() + '&divisionId=' + $('#division').val() + '&semId=' + $('#sem').val()))
                    .then($('.btnExcel').attr('href', 'studExcel.php?streamId=' + $('#streams').val() + '&branchId=' + $('#branches').val() + '&batchId=' + $('#batches').val() + '&divisionId=' + $('#division').val() + '&semId=' + $('#sem').val()))
                    .then($('.btnAdd').show())
                    .then($.studentList($('#streams').val(), $('#branches').val(), $('#batches').val(), $('#division').val(), $('#sem').val()));
            }
        });
    });
    $.streamSelection = function(selStreamId) {
        $.ajax({
            url: "searchBranch.php",
            data: {
                branch: <?php echo $branch;?>,
                streamId: selStreamId
            },
            success: function(data) {
                $('#branches').html(data);
            },
            error: function(error) {
                alert(error);
            }
        });
    }
</script>

我的完整代码是html is。

<?php session_start();
if(isset($_SESSION['userId'])) {
    require_once('../../config/connection.php');
    $streamResult = mysql_query("SELECT * FROM streams");
    if(isset($_GET['streamId'])) {
        $stream=$_GET['streamId'];
    }
    if(isset($_GET['branchId'])) {
        $branch=$_GET['branchId'];
    }
    if(isset($_GET['batchId'])) {
        $batch=$_GET['batchId'];
    }
    if(isset($_GET['divisionId'])) {
        $division=$_GET['divisionId'];
    }
    if(isset($_GET['semId'])) {
        $sem=$_GET['semId'];
    }
?>
    <!DOCTYPE html>
    <html lang="eng">
        <head>
            <meta charset="UTF-8">
            <title>Students | Admin | ITBiz ERP</title>
            <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
            <link rel="stylesheet" type="text/css" href="../public/css/bootstrap.min.css" />
            <link rel="stylesheet" type="text/css" href="../public/css/font-awesome.min.css" />
            <link rel="stylesheet" type="text/css" href="../public/css/ionicons.min.css" />
            <link rel="stylesheet" type="text/css" href="../public/css/bootstrap3-wysihtml5.min.css" />
            <link rel="stylesheet" type="text/css" href="../public/css/AdminLTE.css" />
            <link rel="stylesheet" type="text/css" href="../public/css/admin.custom.css" />
            <link rel="shortcut icon" href="../../views/public/img/favicon.ico">
            <script src="../public/js/jquery.min.js"></script>
            <!--[if lt IE 9]>
                <script src="../public/js/html5shiv.js"></script>
                <script src="../public/js/html5shiv.responsive.js"></script>
            <![endif]-->
        </head>
        <body class="skin-blue">
            <?php include '../public/includes/header.php';?>
            <div class="wrapper row-offcanvas row-offcanvas-left">
                <?php include '../public/includes/sidebar.php';?>
                <aside class="right-side">
                    <section class="content">
                        <div class="page-header">
                            <h7>
                                Students
                                <a href="#" class="btn btn-primary pull-right hide btnAdd" style="margin-left:10px;">Add</a>
                                <a href="#" class="btn btn-info pull-right hide btnExcel">Generate Excel</a>
                            </h7>
                        </div>
                        <!-- selection -->
                        <div class="row" id="addStream">
                            <div class="col-lg-12">
                                <?php
                                    if(isset($_SESSION['message'])) {
                                        echo
                                        "<div class='alert alert-success text-center'>
                                            <p><b>".$_SESSION['message']."</b></p>
                                        </div>";
                                        unset($_SESSION['message']);
                                    }
                                    if(isset($_SESSION['error'])) {
                                        echo
                                        "<div class='alert alert-danger text-center'>
                                            <p><b>".$_SESSION['error']."</b></p>
                                        </div>";
                                        unset($_SESSION['error']);
                                    }
                                ?>
                                <form class="" role="form">
                                    <div class="col-lg-2">
                                        <div class="form-group">
                                            <label for="streams">Select Stream</label>
                                            <select name="streams" id="streams" class="form-control">
                                                <option value="">-- Select --</option>
                                                <?php
                                                    while($streamRow = mysql_fetch_array($streamResult)) {
                                                        echo
                                                            "<option value=".$streamRow[0]." id=".$streamRow[0].">".$streamRow[1]."</option>";
                                                    }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="form-group">
                                            <label for="branches">Select Branch</label>
                                            <select name="branches" id="branches" class="form-control">
                                                <option value="">-- Select --</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="form-group">
                                            <label for="batches">Select Batch</label>
                                            <select name="batches" id="batches" class="form-control">
                                                <option value="">-- Select --</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="form-group divBefore">
                                            <label for="divisionBefore">Select Division</label>
                                            <select name="divisionBefore" id="divisionBefore" class="form-control">
                                                <option value="">-- Select --</option>
                                            </select>
                                        </div>

                                        <div class="form-group divAfter hide">
                                            <label for="division">Select Division</label>
                                            <select name="division" id="division" class="form-control">
                                                <option value="">-- Select --</option>
                                                <?php
                                                    $divisionResult = mysql_query("SELECT * FROM division");
                                                    while($divisionRow = mysql_fetch_array($divisionResult)) {
                                                        echo
                                                            "<option value=".$divisionRow[0].">".$divisionRow[1]."</option>";
                                                    }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="form-group semesterBefore">
                                            <label for="semBefore">Select Semester</label>
                                            <select name="semBefore" id="semBefore" class="form-control">
                                                <option value="">-- Select --</option>
                                            </select>
                                        </div>

                                        <div class="form-group semesterAfter hide">
                                            <label for="sem">Select Semester</label>
                                            <select name="sem" id="sem" class="form-control">
                                                <option value="">-- Select --</option>
                                            </select>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <!-- end selection -->

                        <!-- students list -->
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="page-header">
                                    <h6>Students List</h6>
                                </div>
                            </div>
                        </div>

                        <div class="studList"></div>
                        <!-- end students list -->
                    </section>
                </aside>
            </div>
            <?php
                if(strcmp($_SESSION['role']['name'], 'admin') != 0) {
                    echo
                    $streamList = mysql_query("SELECT streams_id FROM streams where name='".$_SESSION['role']['name']."' ");
                    $streamRow = mysql_fetch_array($streamList);
                    $branchResult = mysql_query("SELECT * FROM branches WHERE streams_id=".$streamRow[0]);
                    echo
                    "<script>
                        $(document).ready(function() {
                            $.when($('#streams').val('".$streamRow[0]."'))
                            .then($('#streams').parent('div').parent('div').hide())
                            .then($.streamSelection($('#streams').val()))
                            .then($.listSemester($('#streams').val()));
                        });
                    </script>";
                }
            ?>

            <script src="../public/js/bootstrap.min.js"></script>
            <script src="../public/js/bootstrap3-wysihtml5.all.min.js"></script>
            <script src="../public/js/app.js"></script>
            <script src="../public/js/jquery.validate.js"></script>
            <script src="../public/js/admin.custom.js"></script>
            <script>
                $(document).ready(function() {
                    $('#<?php echo $stream;?>').prop('selected',true);
                    $('.sidebar-menu .system').addClass('active');
                    $('.sidebar-menu .system .student').addClass('active');
                    $(".sidebar-menu .system").tree();
                    $('#streams').change(function() {
                        $('#branches').html("<option value=''>-- Select --</option>");
                        $('#batches').html("<option value=''>-- Select --</option>");
                        $('.divAfter').hide();
                        $('.divBefore').show();
                        $('.semesterAfter').hide();
                        $('.semesterBefore').show();
                        $('.btnAdd').hide();
                        $('.btnExcel').hide();
                        $('.studList').hide();
                        if($(this).val() == '') {
                        }
                        else {
                            $.when($.streamSelection($(this).val())).then($.listSemester($(this).val()));
                        }
                    });
                    $('#branches').change(function() {
                        $('#batches').html("<option value=''>-- Select --</option>");
                        $('.divAfter').hide();
                        $('.divBefore').show();
                        $('.semesterAfter').hide();
                        $('.semesterBefore').show();
                        $('.btnAdd').hide();
                        $('.btnExcel').hide();
                        $('.studList').hide();
                        if($(this).val() == '') {
                        }
                        else {
                            $.branchSelection($(this).val());
                        }
                    });
                    $('#batches').change(function() {
                        if($(this).val() == '') {
                            $('.divAfter').hide();
                            $('.divBefore').show();
                            $('.semesterAfter').hide();
                            $('.semesterBefore').show();
                            $('.btnAdd').hide();
                            $('.btnExcel').hide();
                            $('.studList').hide();
                        }
                        else {
                            $('.divBefore').hide();
                            $('#division').val('');
                            $('.divAfter').show();
                        }
                    });
                    $('#division').change(function() {
                        $('.studList').hide();
                        $('.semesterAfter').hide();
                        $('.semesterBefore').show();
                        $('.btnAdd').hide();
                        $('.btnExcel').hide();
                        if($(this).val() != '') {
                            $('.semesterBefore').hide();
                            $('#sem').val('');
                            $('.semesterAfter').show();
                        }
                    });
                    $('#sem').change(function() {
                        $('.btnAdd').hide();
                        $('.btnExcel').hide();
                        $('.studList').hide();
                        if($(this).val() != '') {
                            $.when($('.btnAdd').attr('href', 'add.php?streamId='+$('#streams').val()+'&branchId='+$('#branches').val()+'&batchId='+$('#batches').val()+'&divisionId='+$('#division').val()+'&semId='+$('#sem').val()))
                            .then($('.btnExcel').attr('href', 'studExcel.php?streamId='+$('#streams').val()+'&branchId='+$('#branches').val()+'&batchId='+$('#batches').val()+'&divisionId='+$('#division').val()+'&semId='+$('#sem').val()))
                            .then($('.btnAdd').show())
                            .then($.studentList($('#streams').val(),$('#branches').val(),$('#batches').val(),$('#division').val(),$('#sem').val()));
                        }
                    });
                });
                $.streamSelection = function(selStreamId) {
                    $.ajax({
                        url:"searchBranch.php",
                        data:{
                            branch:<?php echo $branch;?>,
                            streamId:selStreamId
                        },
                        success: function(data) {
                            $('#branches').html(data);
                        },
                        error: function(error) {
                            alert(error);
                        }
                    });
                }
                $.branchSelection = function(selBranchId) {
                    $.ajax({
                        url:"searchBatch.php",
                        data:{
                            branchId:selBranchId,
                        },
                        success: function(data) {
                            $('#batches').html(data);
                        },
                        error: function(error) {
                            alert(error);
                        }
                    });
                }
                $.listSemester = function(selStreamId) {
                    $.ajax({
                        url:"searchStream.php",
                        data:{
                            streamId:selStreamId,
                        },
                        success: function(data) {
                            $('#sem').html(data);
                        },
                        error: function(error) {
                            alert(error);
                        }
                    });
                }
                $.studentList = function(streamId,branchId,batchId,divisionId,semId) {
                    $.ajax({
                        url:"searchStudent.php",
                        data:{
                            selStreamId     :   streamId,
                            selBranchId     :   branchId,
                            selBatchId      :   batchId,
                            selDivisionId   :   divisionId,
                            selSemId        :   semId
                        },
                        success: function(data) {
                            $('.studList').html(data);
                            $('.studList').show();
                            if(data.length > 0)
                                $('.btnExcel').show();
                        },
                        error: function(error) {
                            alert(error);
                        }
                    });
                }
            </script>
        </body>
    </html>
<?php
}
else
    header('location:../../');
?>

3 个答案:

答案 0 :(得分:2)

尝试Jquery触发器功能

$(document).ready(function() {
   $( "#streams" ).trigger( "change" );
});

您可以在此处获取有关此内容的更多信息 http://api.jquery.com/trigger/

答案 1 :(得分:2)

在更改事件中创建代码功能,并在您希望它调用的任何地方调用,#streams更改的代码如下。

<script>
    $(document).ready(function() {
        $('#<?php echo $stream;?>').prop('selected', true);
        $('.sidebar-menu .system').addClass('active');
        $('.sidebar-menu .system .student').addClass('active');
        $(".sidebar-menu .system").tree();
     $(document)on('change','#streams', handleStreamChange(this));
handleStreamChange($('#streams')) // **Call this on page load, pass as this the streams dropdown**
});

function handleStreamChange(_this) {
            $('#branches').html("<option value=''>-- Select --</option>");
            $('#batches').html("<option value=''>-- Select --</option>");
            $('.divAfter').hide();
            $('.divBefore').show();
            $('.semesterAfter').hide();
            $('.semesterBefore').show();
            $('.btnAdd').hide();
            $('.btnExcel').hide();
            $('.studList').hide();
            if ($(_this).val() == '') {} else {
                $.when($.streamSelection($(_this).val())).then($.listSemester($(_this).val()));
            }
        }

答案 2 :(得分:2)

试试这个:

$(document).on('change', '#streams', function() {
  // Your code
});