我有一个虚拟的html代码,正在测试JS代码:
<!doctype html>
<html lang=“en”>
<head>
<meta charset=“utf-8”>
<meta name=“viewport” content=“width=device-width, initial-scale=1”>
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel=“stylesheet” href=“//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<link rel=“stylesheet” href=“/resources/demos/style.css”>
<script src=“https://code.jquery.com/jquery-1.12.4.js”></script>
<script src=“https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>
<script>
</script>
</head>
<body>
<div class=“ui-widget”>
<label for=“tags”>Input1: </label>
<input id=“dynamic”>
</div>
<div class=“ui-widget”>
<label for=“tags”>Input2: </label>
<input id=“static”>
</div>
<div class=“ui-widget”>
<label for=“input3”>Input3: </label>
<input id=“both”>
</div>
</body>
</html>
该代码具有三个文本框:
动态 静态的 都
对于这些,我想显示不同的自动完成选项:
var dynamic = [ “JavaScript”,“Python”,“Ruby”]
var static = [ “C”,“C++”,“Java”]
var both = [ “JavaScript”,“Python”,“Ruby”,“C”,“C++”,“Java”]
目前我要做的是调用一个JS函数,该函数具有id和options作为参数三次,每种情况一次:
function show_options (field_id, available_options) {
$( `[id*=${field_id}]` ).autocomplete( {
source: available_options,
minLength: 0
})
.focus(function() {
$(this).autocomplete(‘search’, $(this).val())
})
;
} ;
var dynamic = [ “JavaScript”,“Python”,“Ruby”]
var static = [ “C”,“C++”,“Java”]
var both = [ “JavaScript”,“Python”,“Ruby”,“C”,“C++”,“Java”]
show_options(“dynamic”,dynamic)
show_options(“static”,static)
show_options(“both”,both)
这种工作对我来说确实是很不好的做法。
我这样做的原因是因为我事先不知道ID,并且将从外部服务器接收选项。