Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在Twitter引导的输入上设置焦点';前面有打字机吗_Javascript_Jquery_Twitter Bootstrap_Focus_Bootstrap Typeahead - Fatal编程技术网

Javascript 无法在Twitter引导的输入上设置焦点';前面有打字机吗

Javascript 无法在Twitter引导的输入上设置焦点';前面有打字机吗,javascript,jquery,twitter-bootstrap,focus,bootstrap-typeahead,Javascript,Jquery,Twitter Bootstrap,Focus,Bootstrap Typeahead,删除这些属性时: data-source="<?=str_replace('"', '&quot;', json_encode($equipment, JSON_HEX_TAG | JSON_HEX_APOS)); ?>" data-items="4" data-provide="typeahead" 这是我的HTML标记: <div class="itemx"> <input type="text" class="input-large firs

删除这些属性时:

data-source="<?=str_replace('"', '&quot;', json_encode($equipment, JSON_HEX_TAG | JSON_HEX_APOS)); ?>" data-items="4" data-provide="typeahead"
这是我的HTML标记:

<div class="itemx">
    <input type="text" class="input-large first" autocomplete="off" placeholder="Equipment Name/Label" name="equip[]" data-source="<?=str_replace('"', '&quot;', json_encode($equipment, JSON_HEX_TAG | JSON_HEX_APOS)); ?>" data-items="4" data-provide="typeahead" />
    <input type="text" class="input-large second" placeholder="Quantity" name="quant[]" />
</div>


也许输入是隐藏的

$('input:visible').focus();

Typeahead正在积极阻止默认焦点事件。我不知道为什么(也许有一个用例,焦点在前面中断了,我还没有看到)

在中(在第316至321行末端):

改为:

  $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
    var $this = $(this)
    if ($this.data('typeahead')) return true
    $this.typeahead($this.data())
  })
返回true允许焦点事件完成(仅在建立typeahead时在第一次焦点之后运行),删除
e.preventDefault()
调用允许在初始设置时触发焦点


我有一个关于变更的建议-将用作者的回复更新答案

我最近遇到了这个问题。当我首先声明
.typeahead()
代码块,然后给出字段焦点时,它修复了自己。如果我颠倒顺序,则
.typeahead()
功能将中断

$('#myField').typeahead({
    ...
});

$('#myField').focus();

这取决于您使用的版本,我在0.9.3中遇到了这个问题

不是最干净的,但您可以尝试设置超时:


setTimeout(函数(){$('.first').focus();},0)

使用typeahead.js 0.10.2邪恶壁橱猴子指定的解决方案几乎对我有效。按照规定,您必须在调用“typeahead”后设置焦点,但请记住,“typeahead”调用将操作DOM并添加其他元素。调用“typeahead”后,找到表示输入框的元素并将焦点设置为该元素。如下所示,输入元素被分配了类“tt input”

例如:

html:


typeahead有自己版本的聚焦方法。焦点事件的默认行为被阻止,所以唯一的方法是保持typeahead引用

var typeaheadReference = $('.typeahead').typeahead({ ... };

然后您只需调用
typeaheadReference.focus()

我创建了一个JSFIDLE,供人们快速测试库的版本

我意识到我的另一段代码是禁用我的输入框导致焦点失败,但这帮助我意识到这是我的代码,而不是库

$('#locationSearchText').typeahead({
            minLength: 1,
            highlight: true
        },
        {
            source: function (query, process) {
                return process([{value:'abcdefg'}]);
            }
        });

$('#locationSearchText').focus().typeahead('val', 'abc');

别问我为什么,但这是唯一对我有用的东西:

setTimeout("$('[name=search_input]').focus();", 0)

我最终使用的是点击事件而不是焦点,它对我很有效。

+1这很有效。使用html5
似乎也能很好地工作。我重复一遍,决不在输入元素上使用自动对焦属性和typeahead。它可以在任何地方使用,但不能在iPhone上使用。你真的会把头发拔出来!旧答案,但直到今天(2020年2月),我还没有找到好的理由来解释为什么会这样:b
<div id="levelSearchBox" style="clear: both">
    <input class="typeahead" type="text" placeholder="Search" data-bind="value: selectedRootLevel" />
</div>
    $('#levelSearchBox .typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 0,
        autoselect: true
    }, {
        displayKey: 'name',
        source: levels.ttAdapter()
    }).on('typeahead:selected', function ($e, datum) {
        selectedCallBack(datum["name"]);
    });

    $('#levelSearchBox .tt-input').focus();
var typeaheadReference = $('.typeahead').typeahead({ ... };
$('#locationSearchText').typeahead({
            minLength: 1,
            highlight: true
        },
        {
            source: function (query, process) {
                return process([{value:'abcdefg'}]);
            }
        });

$('#locationSearchText').focus().typeahead('val', 'abc');
setTimeout("$('[name=search_input]').focus();", 0)