我当前正在使用GenemuFormBundle选择一个实体。假设我想选择实体“西瓜”。如果我只是在输入中输入"w“,它会显示一个列表,并且我每键入一个字母,列表就会变短,直到只显示”西瓜“。
现在假设我输入"waterme",列表中目前显示的唯一结果是“西瓜”,如果我提交表单,它将查找一个名为“waterme”的实体,找不到它,并返回"NULL“。
所以,我的问题是,当我的用户验证表单或关注另一个输入时,是否可以自动选择列表中显示的第一个结果?
发布于 2015-08-03 18:07:07
这有点复杂,你必须将提交下拉列表的状态保存在某个地方,当表单是提交时,选择你想要的元素,如果在表单是submitted.Here之前没有选择任何元素,它会这样:
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);
});https://stackoverflow.com/questions/31777387
复制相似问题