cuốn sách gpt4 ai đã làm

javascript - 依赖/级联下拉菜单

In lại Tác giả: Walker 123 更新时间:2023-11-28 08:30:50 28 4
mua khóa gpt4 Nike

我正在使用级联下拉 jquery 插件。 (https://github.com/dnasir/jquery-cascading-dropdown)

我有两个下拉菜单。 “客户端”和“站点”。

根据您选择的客户端,站点列表应适当减少,以仅显示所选客户端的站点。我设置了两个 php 脚本 returnClientList.php 和 returnSiteList.php,它们成功返回带有标签/值对的 json 数组。

我的问题是选择客户端后无法减少站点列表。该事件成功触发,但我只得到完整列表。正如您将看到的,代码使用 getJSON 请求,我从手册中知道该请求发送 HTTP GET。查看 chrome 的网络面板发现实际上没有发送 GET 值。

希望这是显而易见的事情,但我对 jquery 很陌生,所以感谢您的帮助。

我的代码:

JS

$('#edit-shift').cascadingDropdown({
selectBoxes: [
{
selector: '.clients',
source: function(request, response) {
$.getJSON('returnClientList.php', request, function(data) {
var selectOnlyOption = data.length <= 1;
response($.map(data, function(item, index) {
trở lại {
label: item.label,
value: item.value,
selected: selectOnlyOption // Select if only option
};
}));
});
}
},
{
selector: '.sites',
requires: ['.clients'],
source: function(request, response) {
$.getJSON('returnSiteList.php', request, function(data) {
var selectOnlyOption = data.length <= 1;
response($.map(data, function(item, index) {
trở lại {
label: item.label,
value: item.value,
selected: selectOnlyOption // Select if only option
};
}));
});

}
},
{
onChange: function(event, value, requiredValues){}
}
]
});

PHP

//this script returns a json array for use in jquery autocomplete fields for site lists...
header('Content-type: application/json');
require("connect.php");

$client_id = $_GET['?'];

//do the query for sites that are active
$sql = "SELECT * FROM site WHERE active=1 AND client_id='$client_id' ORDER BY site_name ASC";
$result = mysql_query($sql) or die('Error: ' . mysql_error());

//loop the results and create php array
while($row = mysql_fetch_array($result)){
$arr[] = array('label' => $row['site_name'], 'value' => $row['id']);
}

echo json_encode($arr);

câu trả lời hay nhất

最终编写了我自己的解决方案并废弃了该插件。享受。

//dynamically returns the sites once the user chooses a client - edit/add shift form
$('.client-id').change(function () {
var selectedClient = $(this).val();
if (selectedClient != null && selectedClient != '') {

$.getJSON('returnSiteList.php', { id: selectedClient },
function (Sites) {
var siteSelect = $('.site-id');
siteSelect.empty();
$.each(Sites, function (index, site) {
siteSelect.append($('', {
value: site.value,
text: site.label
}));
});
});
}
});

关于javascript - 依赖/级联下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21885554/

28 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress