Hướng dẫn how to disable a field in javascript - cách tắt một trường trong javascript

Tôi đang bắt đầu với JavaScript, tôi đã viết chức năng này:

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

Trong đó vô hiệu hóa trường thứ hai có tên Cantidadcopias nếu trường thứ nhất được lấp đầy.

<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>

Nhưng nó không vô hiệu hóa trường thứ hai khi phần đầu tiên được lấp đầy.

Hướng dẫn how to disable a field in javascript - cách tắt một trường trong javascript

Kate Gregory

18.8k8 Huy hiệu vàng56 Huy hiệu bạc85 Huy hiệu Đồng8 gold badges56 silver badges85 bronze badges

Hỏi ngày 15 tháng 10 năm 2012 lúc 13:31Oct 15, 2012 at 13:31

4

Bạn đã nhìn vào bảng điều khiển?

  • Uncaught Syntaxerror: Mã thông báo bất ngờ)
  • Uncaught ReferenceError: vô hiệu hóa không được xác định

Lần đầu tiên bạn gặp lỗi chính tả, bây giờ mã của bạn có thêm

<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>
0

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

Bây giờ vấn đề tiếp theo là bạn không nhìn vào độ dài của giá trị.

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

Vì vậy, mã sẽ giống như

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

Nhưng bây giờ nó được viết như thế nào, một khi nó bị vô hiệu hóa, nó sẽ không được kích hoạt lại.

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

Đã trả lời ngày 15 tháng 10 năm 2012 lúc 13:38Oct 15, 2012 at 13:38

Hướng dẫn how to disable a field in javascript - cách tắt một trường trong javascript

Epascarelloepascarelloepascarello

200K20 Huy hiệu vàng189 Huy hiệu bạc230 Huy hiệu Đồng20 gold badges189 silver badges230 bronze badges

2

Đó là tốt nhất nếu bạn sử dụng

<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>
1 thay vì
<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>
2. Vấn đề là giá trị của đầu vào không được cập nhật trên sự kiện Keydown.

Vĩ cầm

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

JavaScript

function disableField(val) {
    var cantidadCopias = document.getElementById("cantidadCopias");
    cantidadCopias.disabled = ( val.length > 0  );
}

Đã trả lời ngày 15 tháng 10 năm 2012 lúc 13:48Oct 15, 2012 at 13:48

BobbobBob

1.4911 Huy hiệu vàng16 Huy hiệu bạc28 Huy hiệu đồng1 gold badge16 silver badges28 bronze badges

3

JavaScript:

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

HTML:

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

Bạn cũng nên bật lại khi độ dài đầu vào là 0 một lần nữa.

Bên cạnh đó, bạn nên kết nối Onkeyup và không onkeydown.

Bạn có thể thử nó ở đây: jsfiddle.net/dbjfn/

Đã trả lời ngày 15 tháng 10 năm 2012 lúc 13:43Oct 15, 2012 at 13:43

GottzgottzGottZ

4.7561 Huy hiệu vàng39 Huy hiệu bạc45 Huy hiệu đồng1 gold badge39 silver badges45 bronze badges

2