Tom*_*Tom 2 jquery jquery-autocomplete
我在一个输入字段上使用自动完成功能来填充另一个输入字段:
<input id='fruit' name='fruit' type='text'>
<input id='details' name='details' type='text'>
Run Code Online (Sandbox Code Playgroud)
jQuery代码:
var x = ["apple", "kiwi", "lemon"];
$( "#fruit" ).autocomplete({
source: x
});
jQuery(document).on("change","#fruit", function()
{
$.each(x, function(key, value) {
switch(value) {
case "apple":
$('#details').val("Delicious");
break;
case "kiwi":
$('#details').val("Yummy");
break;
case "lemon":
$('#details').val("Sour");
}
});
});
Run Code Online (Sandbox Code Playgroud)
当有人用自动完成选择苹果,猕猴桃或柠檬时,它会用相应的文本填写另一个输入字段.
我有两个问题:
这是一个小提琴
我猜你使用开关有点困难.如果您查看ui文档,您将看到可以使用对象数组.如果我是你,我会把这样的来源:
var x = [
{ label : 'apple', value : 'Delicious' },
{ label : 'kiwi', value : 'Yummy' },
{ label : 'kiwiooo', value : 'aaa' },
{ label : 'lemon', value : 'Sour' }
];
Run Code Online (Sandbox Code Playgroud)
现在你知道,对于某个标签你有一定的价值,而不使用开关和其他东西,所以剩下的代码看起来像
$( "#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;
});
Run Code Online (Sandbox Code Playgroud)
我把return false设置为false,focus因为如果用户选择使用键盘箭头在列表上向下滑动,则在#fruit输入中将显示该值,这不是正常的.
你可以在这里玩一些东西
| 归档时间: |
|
| 查看次数: |
8579 次 |
| 最近记录: |