Javascript 如何在页面加载时调用jquery函数
在我的php页面中,我有四个下拉列表,我希望选择第一个和第二个下拉列表,这些列表来自数据库,但我在选择第二个下拉列表时遇到了问题。当我更改第一个下拉列表的值时,它工作,所以,它工作在值更改上,但当页面加载时,它不工作,我的函数是$.streamSelectionJavascript 如何在页面加载时调用jquery函数,javascript,php,jquery,Javascript,Php,Jquery,在我的php页面中,我有四个下拉列表,我希望选择第一个和第二个下拉列表,这些列表来自数据库,但我在选择第二个下拉列表时遇到了问题。当我更改第一个下拉列表的值时,它工作,所以,它工作在值更改上,但当页面加载时,它不工作,我的函数是$.streamSelection <script> $(document).ready(function() { $('#<?php echo $stream;?>').prop('selected', true);
<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>
$(文档).ready(函数(){
$('#').prop('selected',true);
$('.sidebar menu.system').addClass('active');
$('.sidebar menu.system.student').addClass('active');
$(“.sidebar menu.system”).tree();
$('#streams').change(function(){
$('#branchs').html(“--Select--”);
$('#batches').html(“--Select--”);
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('.btnAdd').hide();
$('.btnExcel').hide();
$('.studList').hide();
if($(this.val()=''){}else{
$.when($.streamSelection($(this.val()))。然后($.listSerm($(this.val()));
}
});
$(“#分支”).change(function(){
$('#batches').html(“--Select--”);
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('.btnAdd').hide();
$('.btnExcel').hide();
$('.studList').hide();
if($(this.val()=''){}else{
$.branchSelection($(this.val());
}
});
$('#批处理')。更改(函数(){
if($(this.val()=''){
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('.btnAdd').hide();
$('.btnExcel').hide();
$('.studList').hide();
}否则{
$('.divBefore').hide();
$('分割').val('');
$('.divAfter').show();
}
});
$('#除法')。更改(函数(){
$('.studList').hide();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('.btnAdd').hide();
$('.btnExcel').hide();
if($(this.val()!=“”){
$('.semesterBefore').hide();
$('#sem').val('');
$('.semesterAfter').show();
}
});
$('#sem')。更改(函数(){
$('.btnAdd').hide();
$('.btnExcel').hide();
$('.studList').hide();
if($(this.val()!=“”){
$.when($('.btnAdd').attr('href','add.php?streamId='+$('.#streams').val()+'&branchId='+$('.#branchs').val()+'&batchId='+$('.#batches').val()+'.&divisionId='+$('.#division').val()+'&semId='+$('.#sem.val())
.then($('.btnExcel').attr('href','studExcel.php?streamId='+$('#streams').val()+'&branchId='+$('#branchs').val()+'&batchId='+$('#batches').val()+'&divisionId='+$('#division').val()+'&semId='+$('.#sem.val())
。然后($('.btnAdd').show()
。然后($.studentList($('#streams').val(),$('#branchs').val(),$('#batches').val(),$('#division').val(),$('#sem').val());
}
});
});
$.streamSelection=函数(selStreamId){
$.ajax({
url:“searchBranch.php”,
数据:{
分行:,
streamId:selStreamId
},
成功:功能(数据){
$('#branchs').html(数据);
},
错误:函数(错误){
警报(错误);
}
});
}
我的完整代码是html
<?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:../../');
?>
学生|管理| IT商务ERP
选择流
--挑选--
选择分支
--挑选--
选择批次
--挑选--
选择部门
--挑选--
$(document).ready(function() {
$( "#streams" ).trigger( "change" );
});
<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()));
}
}
$(document).on('change', '#streams', function() {
// Your code
});