我正在尝试自动完成两个文本框并相互填充。这是我到目前为止的代码,我现在陷入困境。
<script type="text/javascript" language="javascript" src="scripts/jquery-latest.js"> </script>
<script type="text/javascript" language="javascript" src="scripts/jquery.ui.js"></script>
<script type="text/javascript">
$().ready(function() {
$('#compName').keyup(function() {
var txtSrch = $(this).val();
if ( txtSrch && txtSrch.length > 2 ) {
formData = 'vw=getCompany&compID=0&compName=' + txtSrch;
// alert(formData);
$.ajax({
url: 'output.cfm',
data: formData,
type: "post",
cache: false,
success: function(result) {
$('#result').html(result);
},
error: function(xmlHttpRequest, status, err) {
confirm('Error!' + err );
}
});
}
});
});
</script>
<div style="width:400px; padding:20px;">
<p>Company Selection</p>
<label for="compID">Company ID:</label><br/>
<input type="text" name="compID" id="compID" value="" />
<br/>
<label for="compName">Company Name:</label><br/>
<input type="text" name="compName" id="compName" value="" />
<div id="sel-comp" style="clear:both;"></div>
<input type="submit" name="submit" id="submit" value="Submit">
<div id="result"></div>
</div>
output.cfm有:
<script>
var compName = [{"id":"1000","name":"company1|CO|80401-8077|1000"},{"id":"1005","name":"company2|CO|80308-2291|1005"},{"id":"1010","name":"company3|CO|80202-1450|1010"},{"id":"1015","name":"company4|CO|80206-0109|1015"},{"id":"1020","name":"company5|CO|80003-6638|1020"}];
$('##compName').autocomplete({
source: compName
});
</script>
如果有人输入公司ID 100,我想提供这些选项1000,1005,1010,1015和1020以进行自动完成。选择后,我想用公司名称填写该ID的相应公司名称,反之亦然。名称字符串包括公司名称,州,邮政编码和与“|”分隔的公司ID。
有什么想法吗? 提前谢谢。
答案 0 :(得分:1)
您的工作代码jsfiddle。请参阅。
<div style="width:400px; padding:20px;">
<p>Company Selection</p>
<label for="compID">Company ID:</label><br/>
<input type="text" name="compID" id="compID" value="" />
<br/>
<label for="compName">Company Name:</label><br/>
<input type="text" name="compName" id="compName" value="" />
<div id="sel-comp" style="clear:both;"></div>
<input type="submit" name="submit" id="submit" value="Submit">
<div id="result"></div>
</div>
$(function() {
var compName = [{"id":"1000","value":"company1|CO|80401-8077|1000"},{"id":"1005","value":"company2|CO|80308-2291|1005"},{"id":"1010","value":"company3|CO|80202-1450|1010"},{"id":"1015","value":"company4|CO|80206-0109|1015"},{"id":"1020","value":"company5|CO|80003-6638|1020"}];
var source = [ ];
var mapping = { };
for(var i = 0; i <compName .length; ++i) {
source.push(compName [i].id);
mapping[compName[i].id] = compName[i].value;
}
$("#compID").autocomplete({
source: source,
minLength: 1,
select: function(event, ui) {
$("#compName").val(mapping[ui.item.value]);
}
});
});