我有来自 mysql 的数据,需要在下拉列表中显示。代码如下。
该数据值将来自mysql。所以我需要在下拉列表中显示所有这些值。寻找解决方案。
var data = "user data1, user data2, user data3, user data4/ user data6, user data7, user data8/ user data5, user data9";
function studentPopulate(){
$.each(newTotalData, function(key, value) {
$('#sList')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
}
var totalData = [];
var data_array = [];
var arrayVal = [];
var newTotalData = new Array();
function studentInfo(){
$.getJSON("checkData.php", {section_id:section_id, uID: uID}, function(data) {
$.each(data, function(i, user) {
var data = user.groupContent;
data_array = data.split('/').join(',');
totalData.push(data_array);
arrayVal = totalData;
newTotalData = arrayVal[0].split(',');
studentPopulate();
});
});
}
PHP代码在这里:
<?php
include 'connection.php';
$uID = $_GET["uID"];
$cid = $_GET["section_id"];
mysqli_select_db($con, "DB");
$Query="SELECT * from table WHERE uid='".$uID."' and section_id='".$cid."'";
$result = mysqli_query($con, $Query);
$totalRecords = mysqli_num_rows($result);
//echo $totalRecords;
if($totalRecords) {
while ($row = mysqli_fetch_array($result)) {
$returnData[]=array( //for Json data array
'userName' => $row['fullName'],
'groupContent' => $row['groupContent']
);
}
}
mysqli_close($con);
echo json_encode($returnData);
?>
下拉值的变化。我需要找出下拉文本数组。刚刚更新了我的代码。似乎在数组中找不到值。请帮忙。它可以找到 newTotalData 但我需要找出该下拉值的组(数组)。
$('#sList').change(function()
{
var str = $(this).find(":selected").text();
if($.inArray(str,data_array[0])>=0){alert(data_array[0])};
});
}