最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to call jquery function on page load - Stack Overflow

programmeradmin1浏览0评论

in my php page i have four dropdown list and i want first and second dropdown list selected and those are ing from database but i am getting problem in selecting second dropdown. it's working when i change value of first dropdown so, it's working on value change but not when page load and my function for it is $.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>

and my full code is with 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:../../');
?>

in my php page i have four dropdown list and i want first and second dropdown list selected and those are ing from database but i am getting problem in selecting second dropdown. it's working when i change value of first dropdown so, it's working on value change but not when page load and my function for it is $.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>

and my full code is with 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:../../');
?>
Share Improve this question edited Feb 9, 2016 at 5:51 Milap 7,2398 gold badges27 silver badges47 bronze badges asked Feb 9, 2016 at 5:30 Divyesh JesadiyaDivyesh Jesadiya 9074 gold badges38 silver badges73 bronze badges 6
  • Please add a fiddle of your code :) – xzegga Commented Feb 9, 2016 at 5:35
  • @divyesh what is your html code? – mjyazdani Commented Feb 9, 2016 at 5:36
  • i have put my full code. – Divyesh Jesadiya Commented Feb 9, 2016 at 5:42
  • Please update you code in fiddle for better understanding. – Shashank Shah Commented Feb 9, 2016 at 5:44
  • 1 Just an opinion, but I personally think you have an incredible amount of repetition. You should try and e up with a more intuitive way to do your jquery. You could make one function that has some parameters that help direct the Ajax what to do in different circumstances maybe figure a better way to handle same-named selectors, etc. It would probably help with manageability. – Rasclatt Commented Feb 9, 2016 at 5:57
 |  Show 1 more ment

3 Answers 3

Reset to default 2

Make a function of your code inside change events and call wherever and when you want it to call, code for #streams change is below.

<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()));
            }
        }

Try Jquery trigger function

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

You can get more information about this here http://api.jquery./trigger/

Try this :

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

评论列表(0)

  1. 暂无评论