如何清除asp下拉列表并使用ajax填充?
我想做的是让用户更改一个下拉列表,然后调用一个ajax函数,该函数将发布到代码隐藏(vb.net文件)中,然后用函数返回的数据清除并填充另一个asp下拉列表。希望我说得通如何清除asp下拉列表并使用ajax填充?,ajax,vb.net,jquery,drop-down-menu,Ajax,Vb.net,Jquery,Drop Down Menu,我想做的是让用户更改一个下拉列表,然后调用一个ajax函数,该函数将发布到代码隐藏(vb.net文件)中,然后用函数返回的数据清除并填充另一个asp下拉列表。希望我说得通 <script> $(document).ready(function () { $('.manuf').change(function () { $.ajax({ type: "POST",
<script>
$(document).ready(function () {
$('.manuf').change(function () {
$.ajax({
type: "POST",
url: "ajax.aspx/GetModel",
data: '{' +
'ManufID:"' + $('.manuf').val() + '"' +
'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var data = json_parse(msg.d);
if (data.error) {
alert("Error!");
return;
}
alert(data.model);
},
error: function(msg) {
alert('Get Details Failure: ' + msg);
}
});
});
});
</script>
$(文档).ready(函数(){
$('.manuf').change(函数(){
$.ajax({
类型:“POST”,
url:“ajax.aspx/GetModel”,
数据:“{”+
'ManufID:'+$('.manuf').val()+''+
'}',
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(msg){
var data=json_parse(msg.d);
if(data.error){
警报(“错误!”);
返回;
}
警报(data.model);
},
错误:函数(msg){
警报('获取详细信息失败:'+消息);
}
});
});
});
我假设您知道如何通过ajax从后端获取数据。类似这样的事情-
$.ajax({ url :"/website/myurl", type:"POST", dataType:"application/json"; data:"json",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
}, error:function () {
// handle error
}
});
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value", "0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddlExperience")); });
$.ajax({ url :"/website/myurl", type:"POST", contentType:"application/json;", dataType:"json", data : "{ 'id' : '" + $("select[id*=selCurrentManuf]").val() + "' } ",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value","0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("select[id*=selCurrentModel]")); });
}, error:function () {
// handle error
}
});
假设您在一个变量data
中获取它,该变量是一个数组
你可以试试这样的东西-
$.ajax({ url :"/website/myurl", type:"POST", dataType:"application/json"; data:"json",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
}, error:function () {
// handle error
}
});
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value", "0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddlExperience")); });
$.ajax({ url :"/website/myurl", type:"POST", contentType:"application/json;", dataType:"json", data : "{ 'id' : '" + $("select[id*=selCurrentManuf]").val() + "' } ",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value","0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("select[id*=selCurrentModel]")); });
}, error:function () {
// handle error
}
});
$(“选择[id*=selCurrentModel]”。空()。追加($(“”)。属性(“值”,“0”)。文本(“选择…”);
//对于下拉列表,清除所有选项并添加“选择…”作为第一个选项
$.each(数据,函数(i,d){$(“”)attr(“value”,d.k.).text(d.v.).appendTo($(“#ddlExperience”););
//用户$.each迭代
你可以在这些方面试试。。。。希望这有帮助。我的错-您可以这样编写ajax调用-
$.ajax({ url :"/website/myurl", type:"POST", dataType:"application/json"; data:"json",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
}, error:function () {
// handle error
}
});
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value", "0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddlExperience")); });
$.ajax({ url :"/website/myurl", type:"POST", contentType:"application/json;", dataType:"json", data : "{ 'id' : '" + $("select[id*=selCurrentManuf]").val() + "' } ",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value","0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("select[id*=selCurrentModel]")); });
}, error:function () {
// handle error
}
});
$.ajax({url:/website/myurl],type:“POST”,contentType:“application/json;”,dataType:“json”,data:“{'id':'”+$”([select[id*=selCurrentManuf]”).val()+“}”,
成功:函数(返回\u数据){
var data=$.parseJSON(返回_数据);
//将数据内容添加到其他列表的代码
$(“选择[id*=selCurrentModel]”。空()。追加($(“”)attr(“值”,“0”)。文本(“选择…”);
//对于下拉列表,清除所有选项并添加“选择…”作为第一个选项
$.each(数据,函数(i,d){$(“”).attr(“value”,d.k.).text(d.v.).appendTo($($(选择[id*=selCurrentModel]););
},错误:函数(){
//处理错误
}
});
检查此链接1。要获取url,请使用-Page.Resolve(“default.aspx/somefunction”)2。是的,我只是想说,但最好使用ID,因为您可能有几个元素具有该类名,所以,这是不可取的做法。