$(function() {
var services = [
"Water Wash",
"Tyre Puncture"
];
var servicesCar = [
"Car Wash",
"Tyre Puncture",
"Vehicle Diagonstics",
"Other Repairs",
];
$('#ServiceType').autocomplete({
if($('#VehicleType').val() == '4w') {
source: servicesCar,
minLength: 0,
scroll: true
}
else {
source: services,
minLength: 0,
scroll: true
} }).focus(function() {
$(this).autocomplete("search", "");
});
});
我需要检查选项值是2w
还是4w
,并根据该值,我应该显示源标记。但是,此代码不会自动填充输入
<select id="VehicleType" name="VehicleType">
<option value="2w"> Bike </option>
<option value="4w"> Car </option>
</select>
这是我的HTML代码
答案 0 :(得分:2)
<强> HTML 强>
<select id="VehicleType" name="VehicleType">
<option value="2w"> Bike </option>
<option value="4w"> Car </option>
</select>
<input type="text" id="ServiceType" />
<强> JS 强>
$(function() {
var services = [
"Water Wash",
"Tyre Puncture"
];
var servicesCar = [
"Car Wash",
"Tyre Puncture",
"Vehicle Diagonstics",
"Other Repairs",
];
$( "#ServiceType" ).focusin(function() {
if($('#VehicleType').val() == '4w') {
$('#ServiceType').autocomplete({
source: servicesCar,
minLength: 0,
scroll: true
});
}
else {
$('#ServiceType').autocomplete({
source: services,
minLength: 0,
scroll: true
});
}
});
});
参考Fiddle