【问题标题】:Focus on readonly input专注于只读输入
【发布时间】:2015-07-14 19:52:13
【问题描述】:

我有一个包含三个输入的表单,其中最后一个具有readonly 属性,因此缺少焦点,因为用户填写最后一个输入的方式是单击一些按钮,例如虚拟键盘。所以一切正常。但我试图按enter 并提交我的表单,但由于我在第二次输入后失去了焦点,我的提交不起作用。我该如何解决这个问题?

form.html

<form name="myForm"
          role="form" 
          ng-submit="signin()" 
          novalidate>

        <input type="text" name="input1"/>
        <input type="text" name="input2"/>

        <div ng-controller="mycontroller">
            <span ng-click="fillInput3()">A</span>
            <span ng-click="fillInput3()">B</span>
            <span ng-click="fillInput3()">C</span>
        </div>
        <input type="text" name="input3" readonly=""/>

        <button type="submit">Ok</button>
    </form>

【问题讨论】:

  • 我从未使用过angularjs,但在纯html和javascript中,您可以设置tabindex的控件。
  • 里卡多,更新了我的答案!
  • @threeFourOneSixOneThree 但在某些情况下,我在最后一次输入中没有按 tab 键,因为此输入由按钮的操作填充。

标签: javascript jquery html angularjs forms


【解决方案1】:

试试这个,

给你的按钮一个 id,

<button type="submit" id="submitButton">Ok</button>

在您的控制器中,插入 $window 作为依赖项,

app.controller('yourController', function($scope, $window){

})

然后在fillInput3()函数中写这个,

    var element = $window.document.getElementById("submitButton");
    if(element)
    element.focus();

这将在您退出 fillInput3() 函数时聚焦“提交”按钮。

【讨论】:

  • 这里的结果相同 :(,使用 $window 或使用 $jquery 清理控制台不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多