简体   繁体   English

焦点自动标签Java脚本中删除只读属性

[英]Remove read-only attribute at focus auto-tab java script

I have some sort of form, which prevent to fill the second input text before filling the first input, here is my code 我有某种形式,可以防止在填充第一个输入之前填充第二个输入文本,这是我的代码

 function autotab(current,to){ if (current.getAttribute && current.value.length==current.getAttribute("maxlength")) { //to.removeAttr('readonly'); to.focus(); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="answers"> <input type="text" id="i1" name="i1" size=1 onKeyup="autotab(this, i2)" maxlength=1 autofocus><br> <input type="text" id="i2" name="i2" size=1 onKeyup="autotab(this, i3)" maxlength=1 readonly><br> <input type="text" id="i3" name="i3" size=1 onKeyup="autotab(this, i4)" maxlength=1 readonly><br> 

what I really want is to remove readonly attribute, I'm using the to.removeAttr('readonly') but it show error Uncaught TypeError: to.removeAttr is not a function I already tried to use to.prop('readonly', false); 我真正想要的是删除readonly属性,我正在使用to.removeAttr('readonly')但显示错误Uncaught TypeError: to.removeAttr is not a function我已经尝试使用过Uncaught TypeError: to.removeAttr is not a function to.prop('readonly', false); but it doesn't change anything, any advice? 但这并没有改变任何建议?

The vanilla Javascript method is called removeAttribute : 原始的Javascript方法称为removeAttribute

to.removeAttribute('readonly');

If you wanted to use removeAttr , you would have to convert the element to a jQuery collection first: 如果要使用removeAttr ,则必须首先将元素转换为jQuery集合:

 function autotab(current, to) { if (current.getAttribute && current.value.length == current.getAttribute("maxlength")) { $(to).removeAttr('readonly'); to.focus(); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="answers"> <input type="text" id="i1" name="i1" size=1 onKeyup="autotab(this, i2)" maxlength=1 autofocus><br> <input type="text" id="i2" name="i2" size=1 onKeyup="autotab(this, i3)" maxlength=1 readonly><br> <input type="text" id="i3" name="i3" size=1 onKeyup="autotab(this, i4)" maxlength=1 readonly><br> 

You're also relying on i2 and so on referring to the elements in the inline handlers. 您还依赖于i2 ,因此还要引用内联处理程序中的元素。 Better not to rely on that (see this question for details) - better to select the elements explicitly instead, and to assign the handlers properly using Javascript (inline handlers are generally considered to be pretty poor practice). 最好不要依赖于此 (有关详细信息,请参阅此问题 )-最好改为显式选择元素,并使用Javascript正确分配处理程序(通常认为内联处理程序是非常差的做法)。 Here's one possible way of refactoring it: 这是一种重构方法:

 const answers = document.querySelector('.answers'); answers.addEventListener('keyup', ({ target }) => { if (target.value.length != target.getAttribute('maxlength')) return; const next = target.nextElementSibling; if (!next) return; next.removeAttribute('readonly'); next.focus(); }); 
 input { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="answers"> <input type="text" id="i1" name="i1" size=1 maxlength=1 autofocus> <input type="text" id="i2" name="i2" size=1 maxlength=1 readonly> <input type="text" id="i3" name="i3" size=1 maxlength=1 readonly> 

to is a DOM object. to是一个DOM对象。 Wrap it into $() will fix your issue: 将其包装到$()中将解决您的问题:

 function autotab(current,to){ if (current.getAttribute && current.value.length==current.getAttribute("maxlength")) { $(to).removeAttr('readonly'); to.focus(); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="answers"> <input type="text" id="i1" name="i1" size=1 onKeyup="autotab(this, i2)" maxlength=1 autofocus><br> <input type="text" id="i2" name="i2" size=1 onKeyup="autotab(this, i3)" maxlength=1 readonly><br> <input type="text" id="i3" name="i3" size=1 onKeyup="autotab(this, i4)" maxlength=1 readonly><br> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM