Jquery UI 自动完成额外参数和自动填充 - 一个令人沮丧的解决方案
一直在研究 Jquery UI 的自动完成 (v1.8.5),并意识到严重缺乏有关发送额外参数和拍摄额外数据以自动填充其他字段的文档。我所拥有的有效,但说真的,看起来像是一个黑客......关于如何改进这个有什么想法吗?
<script type="text/javascript">
var optofirst = {
width: 375,
// doing "$(this)" here fails
source: function( request, response ) {
// grab the calling element
// "$(this)" here works but ya gotta dig to get to the ID
var cat = $(this);
var callid = cat[0].element.context.id; //digging away
$.ajax({
// doing "$(this)" here fails
url: "automagic.php",
dataType: "json",
data: {
term : request.term,
//send its ID to the php script
grab : callid,
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
// start assigning item handles to the response
label: item.first,
value: item.first,
last: item.last,
}
}));
}
});
},
select: function( event, ui ) {
console.log( ui.item ?
"Selected: " + ui.item.last :
"Nothing selected, input was " + this.value);
// make #lname have the value last name
// the "item" in this case appears to get its info from the handles assign in "success:"
$("#flyover #lname").attr("value",ui.item.last);
},
minLength: 2,
};
$("#flyover #fname").autocomplete(optofirst);
</script>
Been looking into Jquery UI's Autocomplete (v1.8.5) and realized there is a sever lack of documentation on sending extra parameters and shooting extra data to autofill other fields. What I have works, but seriously, seems like such a hack... Any thoughts on how to improve this?
<script type="text/javascript">
var optofirst = {
width: 375,
// doing "$(this)" here fails
source: function( request, response ) {
// grab the calling element
// "$(this)" here works but ya gotta dig to get to the ID
var cat = $(this);
var callid = cat[0].element.context.id; //digging away
$.ajax({
// doing "$(this)" here fails
url: "automagic.php",
dataType: "json",
data: {
term : request.term,
//send its ID to the php script
grab : callid,
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
// start assigning item handles to the response
label: item.first,
value: item.first,
last: item.last,
}
}));
}
});
},
select: function( event, ui ) {
console.log( ui.item ?
"Selected: " + ui.item.last :
"Nothing selected, input was " + this.value);
// make #lname have the value last name
// the "item" in this case appears to get its info from the handles assign in "success:"
$("#flyover #lname").attr("value",ui.item.last);
},
minLength: 2,
};
$("#flyover #fname").autocomplete(optofirst);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用源属性。设置一个函数而不是 url。
您应该只需要编辑 url 并替换要发送到服务器的任意数量的自定义属性的 customData 行。
例如:
You can use the source property. Set a function instead of an url.
You should need only to edit url and replace customData line for as many custom properties you want to send to the server.
For example:
总体想法对我来说看起来不错。您的代码非常接近 jQueryUI 的自定义数据和显示演示。
不过,有一些事情您可以改进:
// 在自动完成选项对象和 AJAX 调用中执行“$(this)”都会失败
,因为this
在 JavaScript 中,对象字面量没有意义;它包含函数调用的上下文(请参阅这个问题 对此进行了很好的解释);source
函数中,this
可以工作,因为现在它周围有一个函数。this
有一个上下文。而不是:
你可以这样写:
这是因为在本例中
this
已经是一个 jQuery 对象。$(this)
是多余的。此外,element
属性也是一个 jQuery 对象,因此您可以使用attr()
访问
id
其余的对我来说看起来没问题。看看我引用的演示——它做了一些与您想要完成的事情类似的事情。
The general idea looks good to me. Your code is pretty close to jQueryUI's custom data and display demo.
There are a few things you could improve on though:
// doing "$(this)" here fails
both inside the options object for autocomplete and your AJAX call, becausethis
in JavaScript does not make sense in object literals; it contains the context of a function call (see this question for a great explanation ofthis
);source
function,this
works because now there's a function around it.this
has a context.Instead of:
You could write:
This is because
this
in this case is already a jQuery object.$(this)
is redundant. Also, theelement
property is also a jQuery object, so you can just access theid
usingattr()
The rest looks ok to me. Have a look at the demo I referenced--it does some similar things to what you're trying to accomplish.
我真的很想在 Rails 3.1 中使用 CoffeeScript 来做到这一点
这是 github 上的要点
I really wanted to do this in rails 3.1 using coffeescript
Heres a gist for it on github