Jsp中的jquery自动完成

时间:2015-03-05 16:47:59

标签: javascript jsp autocomplete

我是jsp的新手,所以在javascript中,我正在尝试使用两个文本字段创建自动完成表单。另外,我希望第二个根据第一个值自动获取一些值。好吧,我做了一点研究,我在另一个主题中找到了一个代码片段。我认为,当我把它放在netbeans的单个jsp页面中时,它不起作用。我认为缺少了一些东西。你能帮忙吗?感谢。

这是我的代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<body>

<input id='fruit' name='fruit' type='text'>
<input id='details' name='details' type='text'>

<script>

var x = [
{ label : 'apple', value : 'Delicious' },
{ label : 'kiwi', value : 'Yummy' },
{ label : 'kiwiooo', value : 'aaa' },
{ label :  'lemon', value : 'Sour' }
];

$( "#fruit" ).autocomplete({
source: x,
focus : function(){ return false; }
})
.on( 'autocompleteresponse autocompleteselect', function( e, ui ){
var t = $(this),
details = $('#details'),
label = ( e.type === 'autocompleteresponse' ? ui.content[0].label :  ui.item.label ),
value = ( e.type === 'autocompleteresponse' ? ui.content[0].value : ui.item.value );
t.val( label );
details.val( value );
return false;
});

</script>

</body>
</html>

FIDDLE 修订版

0 个答案:

没有答案