我正在使用
jquery 1.8.2和
jqueryui 1.9.0库.
我试图在我的表中添加一个“编辑记录”对话框,其中包含一个jquery-ui的自动完成文本框.
我还使用自动完成的更改事件来限制用户从给定列表中选择项目.
我的问题是:当我显示我的编辑表单时,我将现有数据设置为文本框,但是当用户单击编辑表单中的另一个字段时,更改控制器弹出没有任何更改的错误.我找到了许多样本,显示如何设置组合框类型自动完成的值,但我找不到一个文本框类型.请帮助我解决这个问题,我相信你的答案也会对其他用户有用.
这是我的自动填充文本框的代码:
$("#EditMaterialName").autocomplete({
source: function (request,response) {
$.ajax({
type: "POST",url: "someurl",dataType: "json",data: {
search_string: request.term
},success: function (data) {
response($.map(data,function (item) {
return {
label: item.description,value: item.description
}
}));
}
});
},minLength: 2,select: function (event,ui) {
},change: function (event,ui) {
if (!ui.item) {
$('<div class="error"><b> please select a listed value.</b><div>').insertAfter('#EditMatName');
$('#EditMaterialName').val('');
}
},open: function () {
$(this).removeClass('ui-corner-all').addClass('ui-corner-top');
},close: function () {
$(this).removeClass('ui-corner-top').addClass('ui-corner-all');
}
});
这是显示“编辑记录”对话框的代码:
$('a.edit_link').live("click",function () {
clearEditMaterialForm();
//loading selected value
var valMaterialName = $(this).closest('tr').children('td.cl_material_name').text();
$('#EditMaterialName').val(valMaterialName);
});
非常感谢你的回答.
解决方法
这不是很漂亮,也许它不正确但它的工作原理:
function setAutocompletCurrentValue(id,value) {
$(id).val(value);
var textToShow = $(id).find(":selected").text();
$(id).parent().find("span").find("input").val(textToShow);
}
希望能帮助到你.