GenemuFormBundle自动完成结果

时间:2015-08-02 22:48:41

标签: jquery forms symfony autocomplete

我目前正在使用GenemuFormBundle来选择一个实体。让我们说我想选择实体"西瓜"。如果我只输入" w"在输入中,它显示一个列表,并且对于我键入的每个其他字母,列表缩短,直到它只显示"西瓜"。

现在让我们输入" waterme",并且唯一的结果显示在"西瓜"现在,我提交表单,它会寻找一个名为" waterme"的实体,找不到它,然后返回" NULL"。

所以,我的问题是,当我的用户验证表单或聚焦另一个输入时,是否可以自动选择列表中显示的第一个结果?

1 个答案:

答案 0 :(得分:0)

你必须保持select2下拉列表的某个位置有点复杂,当表单提交时,如果在提交表单之前没有选择任何元素,则选择你想要的元素。这样:< / p>

var currentChoice = null;
var $form = $("#your-form");
var $s2 = $("#your-select2-element");
$s2.on("select2-highlighting", function(event){
  // this event is fired, when some choice is highlighted
  // now store the current choice so we can retrieve it later
  currentChoice = event.val;
});
$form.on('submit', function(){
  //select something if it is not selected, and we have some highlighted choice
  if (!$s2.select2('val') && currentChoice)
    $s2.select2('val',currentChoice);
});