在jqueryajax中使用回调来设置在下拉菜单中选择的选定文本
use call back in jquery ajax to set a selected text selected in drop down
我正在填充一个国家/地区/州和城市下拉列表,但我可以填充下拉列表中的数据,但我想要的是设置我从数据库请求的选项的选定值,但我无法做到这一点,我所做的是使用回调函数。也许可以在不使用回电的情况下完成。下面是我的代码。var状态;
function func(state) {
//alert("callback " + state);
$('#state option[value="' + state + '"]').prop('selected', 'selected');
}
$.ajax({
url: 'http://localhost:3000/getcountrylist',
type: 'GET',
success: function (data, textStatus, jqXHR) {
var countryDropDown = $('#country');
var citizenCountryDropDown = $('#citizencoutnry');
//alert("In country list");
$.each(data, function (index, result) {
countryDropDown.append(
$('<option/>', {
value: result.countrycode,
text: result.countryname
}));
citizenCountryDropDown.append(
$('<option/>', {
value: result.countrycode,
text: result.countryname
}));
});
$.ajax({
url: 'http://localhost:3000/editempform?userid=' + split,
type: 'GET',
success: function (data, textStatus, jqXHR) {
$.each(data, function (index, result) {
//$('#country option[value="PK"]').prop('selected', 'selected');
//$('select[name="country"] option[value="PK"]')
$("#country").find("option").filter(function (index) {
return result.country === $(this).text();
}).prop("selected", "selected");
$.ajax({
url: 'http://localhost:3000/getstatelist',
type: 'GET',
data: {
'countrycode': $("#country").val()
},
success: function (data, textStatus, jqXHR, func) {
var stateDropDown = $('#state');
$.each(data, function (index, result) {
stateDropDown.append(
$('<option/>', {
value: result.state,
text: result.state
}));
});
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
state = result.state;
$.ajax({
url: 'http://localhost:3000/getcitylist',
type: 'GET',
data: {
'countrycode': $("#country").val()
},
success: function (data, textStatus, jqXHR) {
var cityDropDown = $('#city');
$.each(data, function (index, result) {
cityDropDown.append(
$('<option/>', {
value: result.cityname,
text: result.cityname
}));
});
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
$("#city").find("option").filter(function (index) {
return result.city === $(this).text();
}).prop("selected", "selected");
});
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
请更改您选择的jquery下拉脚本,如下所示。
$("#<%= lstAssignedTo.ClientID %> option").filter(function () {
return ($(this).text() == result.d[0]['assigned']);
}).attr('selected', 'selected');
相关文章:
- 用于选择/文本框操作的JavaScript
- 以编程方式选择文本Mobile Safari
- Firefox输入可以't在选择文本时滚动
- 如何在javascript中取消选择文本框
- 如何在html中打印选择文本
- SVG元素——处理和选择文本
- 选择文本并添加到本地存储
- 选择“p文本jquery”
- 为什么火狐在我更改 innerHTML 时会选择文本
- 根据换行符选择文本
- 如何按从当前单词到段落中该句子(.)结尾的范围选择文本
- 单击按钮时选择文本
- JavaScript 触发器事件在 Android 上选择文本
- .val() 选择文本而不是值字符串,当网页动态编辑源 html
- 为什么不用特殊字符替换选择文本,以及如何删除此特殊字符
- 使用链接多次选择文本
- 使用jQuery在焦点上选择文本框是't在移动浏览器中工作
- Safari存在文本输入问题,用户输入时会选择文本,导致文本丢失
- Bootstrap下拉菜单-仅复选框不选择文本
- 使用.on的多个下拉菜单选择文本仅适用于第一个下拉菜单