猿问

如何根据从上一个 [above] 下拉列表中选择的值填充 select2 下拉列表中的选项?

我想使用 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

}

});

} );

凤凰求蛊
浏览 180回答 2
2回答

Helenr

您需要添加带有公司 ID 的 select2 帖子数据:$("#selUser").select2({&nbsp; &nbsp; ajax: {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; url: "getData.php",&nbsp; &nbsp; &nbsp; &nbsp; type: "post",&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; delay: 250,&nbsp; &nbsp; &nbsp; &nbsp; data: function (params) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchTerm: params.term,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compId: $("#company_list").val() //here your company data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; processResults: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results: response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; cache: true&nbsp; &nbsp; }});然后您可以获取公司 ID$_POST["compId"]并将其添加到您对 get data.php 的查询中

Smart猫小萌

快速浏览一下您的代码,我猜您可能遇到了 MySQL 错误。在 getData.php 上,您似乎正在查询名为order的表。由于 order 是MySQL 中的保留字,除非表名包含在反引号中,否则这些查询几乎肯定会抛出错误。例如:$fetchData&nbsp;=&nbsp;mysqli_query($con,"select&nbsp;*&nbsp;from&nbsp;`order`&nbsp;order&nbsp;by&nbsp;id");
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答