在JS

时间:2018-03-02 01:59:44

标签: javascript vue.js focus

我正在尝试单击按钮来更改输入字段焦点。

我正在使用v-bind:autofocus,只需更改box1和box2的值。

<input type="text" placeholder="box1" v-bind:autofocus="box1"> <input type="text" placeholder="box2" v-bind:autofocus="box2">

http://jsfiddle.net/vjvMp/2275/

2 个答案:

答案 0 :(得分:0)

autofocus attribute

  

允许您指定表单控件的输入焦点时   页面加载

您需要在应该获得焦点的元素上调用public class POOrderEntryExt : PXGraphExtension<POOrderEntry> { private bool sendEmailNotification = false; public bool SendEmailNotification { get { return sendEmailNotification; } set { sendEmailNotification = value; } } [PXOverride] public void Persist(Action del) { using (var ts = new PXTransactionScope()) { if (del != null) { del(); } if (SendEmailNotification) { bool sent = false; string sError = "Failed to send E-mail."; try { Notification rowNotification = PXSelect<Notification, Where<Notification.name, Equal<Required<Notification.name>>>> .Select(Base, "PONotification"); if (rowNotification == null) throw new PXException("Notification Template was not found."); var order = Base.Document.Current; var emails = new List<string>(); var requests = PXSelectJoinGroupBy<RQRequest, InnerJoin<RQRequisitionContent, On<RQRequest.orderNbr, Equal<RQRequisitionContent.orderNbr>>>, Where<RQRequisitionContent.reqNbr, Equal<Required<RQRequisition.reqNbr>>>, Aggregate<GroupBy<RQRequest.orderNbr>>> .Select(Base, order.RQReqNbr); foreach (RQRequest request in requests) { if (request.EmployeeID != null) { var requestCache = Base.Caches[typeof(RQRequest)]; requestCache.Current = request; var emplOrCust = (BAccountR)PXSelectorAttribute .Select<RQRequest.employeeID>(requestCache, request); if (emplOrCust != null) { var defEmpContact = (Contact)PXSelectorAttribute .Select<BAccountR.defContactID>( Base.Caches[typeof(BAccountR)], emplOrCust); if (!String.IsNullOrEmpty(defEmpContact.EMail) && !emails.Contains(defEmpContact.EMail)) { emails.Add(defEmpContact.EMail); } } } } foreach (string email in emails) { var sender = TemplateNotificationGenerator.Create(order, rowNotification.NotificationID.Value); sender.RefNoteID = order.NoteID; sender.MailAccountId = rowNotification.NFrom.HasValue ? rowNotification.NFrom.Value : PX.Data.EP.MailAccountManager.DefaultMailAccountID; sender.To = email; sent |= sender.Send().Any(); } } catch (Exception Err) { sent = false; sError = Err.Message; } if (!sent) throw new PXException(sError); } ts.Complete(); } } } 。指令可以做到这一点。我使用focus(),否则初始焦点没有设置正确。

nextTick
var data = {
  whichHasFocus: 'box1'
}

var demo = new Vue({
  el: '#demo',
  data: data,
  methods: {
    changeFocus() {
      this.whichHasFocus = this.whichHasFocus == 'box1' ? 'box2' : 'box1';
    }
  },
  directives: {
    focusIf(el, binding) {
      if (binding.arg === binding.value) {
        Vue.nextTick(() => el.focus());
      }
    }
  }
})

答案 1 :(得分:0)

您可以通过directives定义一个自定义命令,然后在处理程序中,根据数据绑定的状态关注元素(下面是box1 / box2)。

您可以查看Vue Focus进一步研究。

&#13;
&#13;
var demo = new Vue({
    el: '#demo',
    data: {
      box1: true,
      box2: false
    },
    methods: {
    	changeFocus() {
      	this.box1 = false;
        this.box2 = true;
      }
    },
    directives: {
    focus: {
      update: function (el, {value}) {
        if (value) {
          el.focus()
        }
      }
    }
  },
})
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<html>
<body>
<div id="demo">

    <input type="text" placeholder="box1" v-focus="box1" @blur="box1 = false"><br>
    <input type="text" placeholder="box2" v-focus="box2" @blur="box2 = false"><br>
    <button @click="changeFocus()">Change Focus</button> 

</div>
</body>
</html>
&#13;
&#13;
&#13;