jQuery-UI 自动完成提交表单 onclick 下拉列表中的项目
我正在使用 jQueryUI autocomplete()
函数,但我不知道如何在选择项目时提交表单。
我认为问题出在 select: event
上,但我是 jQueryUI 新手,不知道如何完成这项工作。
这是我的代码,否则可以正常工作:
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$( "#search_box" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autocomplete/suggestions'); ?>",
data: { term: $("#search_box").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
},
select: function (event, ui) {
$(event.target).val(ui.item);
$('#search_form').submit();
return false;
}
});
},
minLength: 1
});
});
});
</script>
任何帮助将不胜感激!
I'm using the jQueryUI autocomplete()
function and I can't figure out how to have my form submit when an item is selected.
I think the issue is with the select: event
but I'm new with jQueryUI and can't figure out how to make this work.
Here's my code which works fine otherwise:
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$( "#search_box" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autocomplete/suggestions'); ?>",
data: { term: $("#search_box").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
},
select: function (event, ui) {
$(event.target).val(ui.item);
$('#search_form').submit();
return false;
}
});
},
minLength: 1
});
});
});
</script>
Any assistance would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
安德鲁是正确的,请参阅他提到的小提琴。如果将“ui.item”部分切换为“ui.item.value”,则 select: function() 现在可以完美运行。
Andrew was correct, see the fiddle he mentioned. If you switch the part with "ui.item" to "ui.item.value" the select: function() now works perfectly.
是的,它有效,
除非您希望在操作调用的页面中获取服务器端脚本中的值...
目前,无法找出为什么在选择后自动提交表单鼠标或键盘,以及
$(event.target).val(ui.item.value)
行,不要传递$_POST
数组中的值Yes it works
Except if you expect to get the value in your server-side script in the page called by action...
For now, no way to find why auto-submit of the form after selection by mouse or by keyboard, and the
$(event.target).val(ui.item.value)
line, don't deliver the value in$_POST
array是的,爱丽丝也是对的。您只需在 onclick 函数中添加以下行:
其中 search_form 是表单的 id。
Yeah Alice is also right. You just need to add this line in your onclick function:
where search_form is the id of your form.