如何禁用使用JavaScript的输入字段? [英] How to disable an input field using Javascript?

查看:106
本文介绍了如何禁用使用JavaScript的输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function disableField(){
if( document.getElementById(valorFinal)。length> 0)){
document.getElementById(cantidadCopias)。disabled = true;






禁用名为cantidadCopias的第二个字段,如果第一个字段充满。

 < label> < span> Valor final:< / span> 
< input type =textclass =input_textname =valorFinalid =valorFinalonkeydown =disableField()/>
< / label>
< label> < span> Cantidad de Copias:< / span>
< input type =textclass =input_textname =cantidadCopiasid =cantidadCopias/>
< / label>

但是当第一个字段填满时,它不会禁用第二个字段。

解决方案


$ b


  • 未捕获的SyntaxError:意外的令牌)

  • 未捕获的ReferenceError:disableField未定义



第一次拼写错误,现在你的代码有一个额外的

  function disableField() {
if(document.getElementById(valorFinal)。length> 0)){< - extra)
document.getElementById(cantidadCopias)。disabled = true;


$ / code>

现在下一个问题是你没有看值的长度。

  if(document.getElementById(valorFinal)。length> 0)<  - 您正在查看长度的HTML DOM节点。 

所以代码看起来应该像



<$ p $ document.getElementById(valorFinal)。value.length> 0){
document.getElementById(cantidadCopias)。 disabled = true;
}
}

但现在如何编写它,一旦禁用,它不会被重新启用。

  function disableField(){
var isDisabled = document.getElementById(valorFinal).value.length> 0;
document.getElementById(cantidadCopias)。disabled = isDisabled;
}


I'm starting with Javascript, I wrote this function:

function disableField() {
  if( document.getElementById("valorFinal").length > 0 ) ) {
    document.getElementById("cantidadCopias").disabled = true; 
  }
}

Which disables the second field named cantidadCopias if the first one is filled.

<label> <span>Valor final:</span>
  <input type="text" class="input_text" name="valorFinal" id="valorFinal" onkeydown="disableField()"/>
</label>
<label> <span>Cantidad de Copias:</span>
  <input type="text" class="input_text" name="cantidadCopias" id="cantidadCopias"/>
</label>

But it's not disabling the second field when the first one is filled.

解决方案

Did you look at the console?

  • Uncaught SyntaxError: Unexpected token )
  • Uncaught ReferenceError: disableField is not defined

First time you had a spelling error, now your code has an extra )

function disableField() {
  if( document.getElementById("valorFinal").length > 0 ) ) {  <-- extra )
    document.getElementById("cantidadCopias").disabled = true; 
  }
}​

Now the next issue is you are not looking at the length of the value.

if( document.getElementById("valorFinal").length > 0 )  <-- you are looking at the length of the HTML DOM Node.

So the code should look like

function disableField() {
  if( document.getElementById("valorFinal").value.length > 0 ) { 
    document.getElementById("cantidadCopias").disabled = true; 
  }
}​

but now how it is written, once it is disabled, it will not be re-enabled.

function disableField() {
    var isDisabled = document.getElementById("valorFinal").value.length > 0; 
    document.getElementById("cantidadCopias").disabled = isDisabled;
}​

这篇关于如何禁用使用JavaScript的输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆