gpt4 book ai didi

javascript - 如何在页面加载时调用jquery函数

转载 作者:行者123 更新时间:2023-11-28 12:22:19 25 4
gpt4 key购买 nike

在我的 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 的。

<?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:../../');
?>

最佳答案

在更改事件中创建一个代码函数,并在您希望它调用的时间和地点进行调用,#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()));
}
}

关于javascript - 如何在页面加载时调用jquery函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com