基于按钮单击的下一个空文本输入填充下一个空文本输入
我有一组简单的按钮,可以按任何顺序单击。单击时,按钮应填写下一个可用的文本框。
到目前为止,我只能使按钮单击填充焦点的文本框。这只能真正完成我任务的一半。
目前,我只是在寻找香草JS解决方案,而不是如果可能的话。
<body>
<div class="buttons">
<button class="btn" id="txt1" onclick="addText('txt1')">txt1</button>
<button class="btn" id="txt2" onclick="addText('txt2')">txt2</button>
<button class="btn" id="txt3" onclick="addText('txt3')">txt3</button>
<button class="btn" id="txt3" onclick="addText('txt3')">txt3</button>
<button class="btn" id="txt4" onclick="addText('txt4')">txt4</button>
<button class="btn" id="txt5" onclick="addText('txt5')">txt5</button>
</div>
<div class="textBoxes">
<input type="text" class="inputs" id="box1" placeholder="WPT 1" onfocus="field=this;" autofocus/>
<input type="text" class="inputs" id="box2" placeholder="WPT 2" onfocus="field=this;"/>
<input type="text" class="inputs" id="box3" placeholder="WPT 3" onfocus="field=this;"/>
<input type="text" class="inputs" id="box4" placeholder="WPT 4" onfocus="field=this;"/>
<input type="text" class="inputs" id="box5" placeholder="WPT 5" onfocus="field=this;"/>
</div>
<script>
var field = 0;
function addText(txt){
if(field === 0) return false;
field.value = txt;
}
</script>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会选择一种基于自定义组件的方法,因此一个人仅在一次 中就实现了尽可能通用的代码。 代码重新使用对于同一任务(甚至相似)的任务非常容易,但具有不同的标记。一个人还可以在每个文档上使用/运行多个组件。
该方法是围绕全局属性, /a> and 。
组件(源)root通过其
[数据prefill-values-for]识别
属性,其中其相关dataset.prefillvaluesfor
value value持有组件的ID。后者将通过`[data-prefill-targets-for =“ $ {componentId}”]`
来查询组件相关的目标根。组件的初始化是通过订阅
bint
的单个处理程序完成的组件目标root到任何'code>'code>'单击'
事件(请参阅... )(请参阅... < a href =“ https://davidwalsh.name/event-delegate” rel =“ nofollow noreferrer”> event-delegation )。处理程序本身从其边界的目标root检索第一个可用的空的预填充目标,该目标是通过查询
[data-prefill-target]
来完成的,并通过其空的value
过滤相关元素/代码>。此外,只有存在一个空的预填充目标,处理程序才从其事件目标中检索到“设置预填充价值”,通过阅读事件目标的dataset.prefillvalue
可以实现。如果满足所有作业标准,则最终进行了任务。I would choose kind of a custom component based approach, thus one implements code as generic as possible just once. Code-reuse will be possible pretty easy for same (even similar) tasks but with e.g. a different markup. One also can use/run more than just one component at each document.
The approach is build around the usage of
data-*
global attributes, attribute selectors andHTMLElement.dataset
.A component (source) root gets identified by its
[data-prefill-values-for]
attribute where its relateddataset.prefillValuesFor
value holds such a component's ID. The latter will be used for querying the component's related target root via`[data-prefill-targets-for="${ componentId }"]`
.The initialization of a component is done by subscribing a single handler which
bind
s the component's target root to any'click'
event from within the root element (see ... event-delegation).The handler itself retrieves from its bound target root the first available empty prefill-target which is done by querying for
[data-prefill-target]
and filtering the related elements each by its emptyvalue
. In addition, but only if an empty prefill-target exists, the handler retrieves from its event-target the to be set prefill-value which gets achieved by reading the event-target'sdataset.prefillValue
. In case all assignment criteria was fulfilled the assignment finally takes place.您可以在功能中添加paramater以更新这样的精确文本框:
然后称其为“ addtext('text',3)
”
如果通过“下一个可用”,您的意思是一个没有值的字段,然后编辑您的功能
:演示检查此沙盒:
You can add a paramater to your function to update exact text box like this:
and then call it like "addText('text', 3)"
Check this sandbox
https://codesandbox.io/s/laughing-einstein-byhf0f?file=/src/index.js:299-472
If by "next available", you meant a field which doesn't already have a value then edit your function like this:
For a demo check this sandbox: https://codesandbox.io/s/trusting-browser-lckvy0?file=/index.html