我的代码确实发生了一些奇怪的事情: 我正在使用jQuery自动完成功能来填写设备表格并通过ajax发送。由于您可以输入多个设备项而无需刷新浏览器,因此在打开带有表单的对话框时,我将清除所有字段。不幸的是,textarea字段已清除,但不会自动完成。令我失望的是,如果我检查文本区域,该区域显示为已填充(见图)。 如果我注释$(“ #text”)。val(''),它实际上可以工作。行(当然,该字段不会重置)。没有显示错误。 是什么导致这种现象? 尽可能精简,下面是代码:
<script>
$(document).ready(function() {
var equipment = [
{
id: "",
label: "",
text: "",
category: "",
rev_type: "",
price: "",
cost: "",
time: ""
},
{
id: "1",
label: "Delete",
text: "",
category: "4",
rev_type: "1",
price: "0.00",
cost: "0",
time: "0"
},
{
id: "32",
label: "Samsung 55\" LED Monitor",
text: "",
category: "3",
rev_type: "1",
price: "650.00",
cost: "150",
time: "60"
},
{
id: "31",
label: "ART Audio DI",
text: "",
category: "4",
rev_type: "1",
price: "55.00",
cost: "0",
time: "0"
},
{
id: "30",
label: "Whirlwind PC Stereo DI",
text: "",
category: "4",
rev_type: "1",
price: "55.00",
cost: "0",
time: "0"
}
];
</script>
<script>
$(document).ready(function(){
updateTotal();
function clearForm() {
// clean fields
$( "#text").val('');
return false;
}
$( document).on( 'click', '.add_edit_equipment', function(){
clearForm();
/* ***** Dialog ***** */
});
});
</script>
<script>
$(document).ready(function() {
$("body").on("focus", ".equipment", function() {
$( this ).autocomplete({
source: equipment,
focus: function( event, ui ) {
$( "#add_edit_form #equipment" ).val( $(this).html(ui.item.label).text() );
return false;
},
select: function( event, ui ) {
$( "#text" ).html( ui.item.text ).text();
itemSubTotal();
return false;
}
})
});
给我一些问题的领域:
<textarea id="text" class="form-control" name="text" cols="55" rows="5" placeholder="Description"></textarea>
答案:我仍然不知道为什么它停止工作并给我这个问题,但是当我替换下一行时,它又开始工作
$( "#text" ).html( ui.item.text ).text();
使用
$( "#text" ).val( ui.item.text );
谢谢大家!