//用server端语言赋值给js变量
var departmentId = '<%=提交的值 %>', deviceId='<%=提交的值 %>'
$(function () {
$.ajax({
url: "deJson.action",
type: 'POST',
dataType: 'JSON',
timeout: 5000,
//error: function() {alert('载入数据异常,请重试!');},
success: function (data) {
//$("#departmentId").empty();
$.each(eval(data), function (i, item) {
$("<option value='" + item.id + "'"+(item.id==departmentId?
' selected':'')+">" + item.name + "</option>").appendTo($("#departmentId"));
});
}
});
$("#departmentId").click(function () {
loadDevice($("#departmentId").val());
});
function loadDevice(departmentId) {
$.ajax({
url: 'dJson.action?
departmentId=' + departmentId,
type: 'POST',
dataType: 'JSON',
timeout: 5000,
error: function () { alert('载入数据异常,请重试!'); },
success: function (msg) {
if (msg == "{[]}") {
$("#deviceId").empty();
$("<option value='0'>查询全部</option>").appendTo($("#deviceId"));
} else {
$("#deviceId").empty();
$("<option value='0'>查询全部</option>").appendTo($("#deviceId"));
$.each(eval(msg), function (i, item) {
$("<option value='" + item.deviceId + "'" + (item.id == deviceId ?
' selected' : '') + ">" + item.name + "</option>").appendTo($("#deviceId"));
});
}
}
});
}
})
用jquery ajax做的二级联动菜单。联动效果实现了,如今想实现另外一个效果,怎样在表单提交后让刚才选择的下拉菜单项为选中状态?
脚本部分