我正在尝试基于从html元素select中选择的值来创建一个switch case但是我似乎遇到了问题,因为代码没有被执行。代码必须类似于“即时搜索”。
$(function(){
var $searchInput = $("#search");
$("#searchOptions").change(function(){
var selection = $("#searchOptions").val();
switch (selection) {
case "Fragment":
$searchInput.keyup(function(e){
var q = $searchInput.val();
$.ajax({
type: 'GET',
url: 'http://localhost:51834/CoffeeService.svc/getaorderfrag/' + q,
success: function(orders){
console.log('success', orders);
$.each(orders, function(i, order){
$("#orderTable tbody").append('<tr><td>' + order.ID +
'</td><td>' + order.Name + '</td><td>' + order.Order + '</td><td>' + order.Price + "-kr;" +'</td></tr>');
});
},
error: function(data){
$searchInput.keyup(function(e){
if(data == "404 (Not Found)"){
$("#orderTable td").remove();
alert("Nothing was found! Sorry :( ");
}
});
}
});
});
break;
case "ID":
$searchInput.keyup(function(e){
var id = $searchInput.val();
$.ajax({
type: 'GET',
url: 'http://localhost:51834/CoffeeService.svc/getaorder/' + id,
success: function(order){
console.log('success', order);
if(!order)
{ $("#orderTable td").remove(); }
else{
$("#orderTable tbody").append('<tr><td>' + order.ID +
'</td><td>' + order.Name + '</td><td>' + order.Order + '</td><td>' + order.Price + "-kr;" +'</td></tr>');
}
},
error: function(data){
if(data == "404 (Not Found)");
$("#orderTable td").remove();
}
});
});
break;
});
});
答案 0 :(得分:1)
首先,我想你可能会搬家了 var selection = $(“#searchOptions”)。val();
到.chage函数范围。现在的方式是,每次更改时都不会捕获新值。