【发布时间】:2012-03-17 13:17:49
【问题描述】:
在你 -1 这是一个如此简单的问题之前,请进一步阅读,因为我花了一段时间搜索但无法弄清楚。我有5个选择框。每个结果取决于之前选择的内容。没有提交按钮。我只需要根据上一个框中选择的内容进行更改,到目前为止我已经有了。哦,结果是从数据库中提取的。问题是,当您在假设框 1 中选择一个选项时,会出现框 2 选项,但是当您再次在框 1 中选择一个选项时,它们的选项只是堆叠在其他选项上。当它再次改变时,我需要他们清除。我会发布我拥有的 js,如果您需要其他任何内容,请询问。
附言我正在使用 php-mysql 获取选项。 p.s.s 这个网站必须在明天之前完成,所以我有点着急。请不要浪费时间告诉我这是一个愚蠢的问题,或者什么不是。
提前感谢您的帮助。
$(function () { //When the page is finished loading run this code
$('#country').change(function () {
$.ajax({
url: "<!--Base url taken out-->/discovery/aLoad",
success: function (data) {
eval(data);
},
error: function () {
alert('failed');
},
data: {
country: getSelectValues('#country')
},
type: 'POST'
});
});
});
function changeSearchBar (newBar) {
//remove the crap
function ClearOptions(country)
{
document.getElementById('country').options.length = 0;
}
for (var i = 0; i <= newBar.length; i++) {
newBar[i].id = newBar[i][0];
newBar[i].name = newBar[i][1];
$('#group').append(
$('<option></option>').attr('value', newBar[i].id).text(newBar[i].name)
);
}
}
function getSelectValues (sId) {
var str = "";
$(sId +" option:selected").each(function () {
str += $(this).val() + ",";
});
return str.replace(/.$/g, '');
}
【问题讨论】:
-
您的 ClearOptions 函数看起来不错,但我看不出您在哪里调用它。它是在 changeSearchBar 中定义的,但从未调用过。我认为如果您在 for 循环之前调用它应该没问题。
标签: javascript jquery ajax onchange html-select