如果条件在jquery ui中自动完成

时间:2016-08-22 11:18:49

标签: jquery jquery-ui autocomplete jquery-ui-autocomplete

$(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代码

1 个答案:

答案 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