【发布时间】:2018-01-31 04:08:49
【问题描述】:
我正在构建一个包含多个输入字段的表单。我目前使用远程 JSON 响应进行自动完成。我想要它,所以当我单击产品名称时,它会为我填写剩余的产品信息,例如描述和价格。文本输入 id 是描述和价格。我怎样才能完成这个?我现在有以下 javascript 代码,它适用于产品名称。
Javascript 文件:
$( document ).ready(function() {
"use strict";
var searchRequest = null;
$("#product-search").autocomplete({
minLength: 3,
source: function(request, response) {
if (searchRequest !== null) {
searchRequest.abort();
}
searchRequest = $.ajax({
url: '/includes/estimate-search.php',
method: 'post',
dataType: "json",
data: {term: request.term},
success: function(data) {
searchRequest = null;
response($.map(data, function(Product) {
return {
value: Product.Name,
label: Product.Name
};
}));
}
}).fail(function() {
searchRequest = null;
});
}
});
});
JSON 响应示例:
[
{
"Name": "Control 4 C4-EA1",
"Description": "Control4 EA-1 Entertainment and Automation Controller",
"SKU": "C4-EA1",
"Cost": "xxx.00 ",
"Price": "xxx.00 "
}
]
HTML 部分:
<table id="estimate" class="dispaly table table-striped">
<thead>
<tr>
<th class="col-md-2">Name</th>
<th class="col-md-4">Description</th>
<th class="col-md-2">SKU</th>
<th class="col-md-2">Cost</th>
<th class="col-md-2">Price</th>
</tr>
</thead>
<tbody id="estimate_row">
<tr>
<td><input type="text" name="product-search" id="product-search" class="form-control typeahead" placeholder="Product Name" /></td>
<td><input type="text" name="description" id="description" class="form-control typeahead" placeholder="Description" /></td>
<td><input type="text" name="sku" id="sku" class="form-control typeahead" placeholder="SKU" /></td>
<td><div class="input-group"><span class="input-group-addon">$</span><input type="text" name="cost" id="cost" class="form-control typeahead" placeholder="Cost" /></div></td>
<td><div class="input-group"><span class="input-group-addon">$</span><input type="text" name="price" id="price" class="form-control typeahead" placeholder="Price" /></td></div></td>
</tr>
</tbody>
</table>
【问题讨论】:
-
能否请您张贴html表单?
-
刚刚添加了我希望在选择产品名称时影响的 html 部分。