我使用下面的代码来填充多个combox,其中的值是从jquery中的ASP.NET MVC后端代码连续返回的:
var province = $("select[name='@(Model.ProvinceFieldName)']");
var provinceValue = '@provinceId';
var city = $("select[name='@(Model.CityFieldName)']");
var center = $("select[name='@(Model.CenterFieldName)']");
var centerType = $("select[name='@(Model.CenterTypeFieldName)']");
var region = $("select[name='@(Model.RegionFieldName)']");
if (provinceValue) {
$("select[name='@(Model.ProvinceFieldName)']").val('@provinceId').trigger('change');
$.fn.whenAction(function () {
return $("select[name='@(Model.ProvinceFieldName)']").val() == '@provinceId' && $("select[name='@(Model.RegionFieldName)']").length > 0 && $("select[name='@(Model.CityFieldName)']").length > 0;
}).then(function () {
debugger
var regionId = $("select[name='@(Model.RegionFieldName)'] option").filter(function () {
return $(this).text() == '@regionName';
}).val();
if (regionId) {
setTimeout(function () {
$("select[name='@(Model.RegionFieldName)']").val(regionId).trigger('change');
setTimeout(function () {
$("select[name='@(Model.CityFieldName)']").val('@cityId').trigger('change');
}, 2000);
}, 2000);
} else {
$("div#error-alert").css({ "display": "block" });
setTimeout(function () {
$("select[name='@(Model.CityFieldName)']").val('@cityId').trigger('change');
}, 2000);
}
$.fn.whenAction(function () {
return $("select[name='@(Model.CityFieldName)']").val() == '@cityId' && $("select[name='@(Model.CenterTypeFieldName)']").length > 0;
}).then(function () {
setTimeout(function () {
$("select[name='@(Model.CenterTypeFieldName)']").val('@centerTypeId').trigger('change');
}, 2000);
$.fn.whenAction(function () {
return $("select[name='@(Model.CenterTypeFieldName)']").val() == '@centerTypeId' && $("select[name='@(Model.CenterFieldName)']").length > 0;
}).then(function () {
setTimeout(function () {
$("select[name='@(Model.CenterFieldName)']").val('@centerId').trigger('change');
}, 2000);
$.fn.whenAction(function () {
return $("select[name='@(Model.CenterFieldName)']").val() != '@centerId' && $("select[name='@(Model.CenterFieldName)']").length > 0;
}).then(function () {
setTimeout(function () {
$("select[name='@(Model.CenterFieldName)']").val('@centerId').trigger('change');
}, 2000);
});
});
});
});
字符串
这是我使用的whenAction函数:
(function ($) {
$.fn.whenAction = function (action, check_freq) {
var freq = check_freq || 500;
var defer = $.Deferred();
var int = setInterval(function () {
if (action() === true) {
clearInterval(int);
defer.resolve();
}
}, freq);
return defer.promise();
}
})(jQuery);
型
但它并不总是正常工作。通常它不能正确地填充组合并且组合保持空和
1条答案
按热度按时间qq24tv8q1#
下面是一个可能的替代代码,它可能更适合您的场景:
字符串