我想使用 select2 js 带来一个带有用户搜索的下拉列表,其中下拉列表中的值必须根据所选公司填充,这是此下拉列表上方的下拉列表。我在 MYSQLI 中使用 XAMPP 版本 5。<?php include 'db.php';?> <html><head> <link href='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css' rel='stylesheet' type='text/css'> <script src="../assets/js/core/jquery.3.2.1.min.js"></script> <link href="../assets/css/bootstrap.min.css" rel="stylesheet" /> <script src="../assets/js/core/bootstrap.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js'></script></head><body><form action="" method="POST" enctype="multipart/form-data" autocomplete="off"><div class="row"><div class="col-sm-2"></div><div class="col-sm-10"><div class="form-group row"><label for="sel1" class="col-sm-2 col-form-label">Company :</label><div class="col-sm-10"><select onChange="company_info();" id="company_list" required class="form-control select2" name="company_id" ><option value="">Select Company</option><?php $companyDetails=mysqli_query($con, "SELECT * FROM company order by id desc ");while($company = mysqli_fetch_assoc($companyDetails)){echo "<option value='".$company['id']."'";echo " >".$company['compName']."</option>";}?> </select></div></div><div id="companyDesc"></div><div class="form-group row"><label for="pthings" class="col-sm-2 col-form-label">To dos :</label><div class="col-sm-10"><select id="selUser" style="width: 200px;"><option value="0">- Search -</option></select></div></div><div class="form-group"><button type="submit" name="add" class="btn btn-default greenbtn btnrightalign">Submit</button></div></div></form></body></html><script>$(document).ready(function() {$("#selUser").select2({ajax: { url: "getData.php",type: "post",dataType: 'json',delay: 250,data: function (params) {return {searchTerm: params.term // search term};},processResults: function (response) {return {results: response};},cache: true}});} );
2 回答
Helenr
TA贡献1780条经验 获得超4个赞
您需要添加带有公司 ID 的 select2 帖子数据:
$("#selUser").select2({
ajax: {
url: "getData.php",
type: "post",
dataType: 'json',
delay: 250,
data: function (params) {
return {
searchTerm: params.term,
compId: $("#company_list").val() //here your company data
};
},
processResults: function (response) {
return {
results: response
};
},
cache: true
}
});
然后您可以获取公司 ID$_POST["compId"]并将其添加到您对 get data.php 的查询中
添加回答
举报
0/150
提交
取消