2011-06-10 76 views
0

我有一個輸入框,我想暫時禁用輸入,直到一個函數在JavaScript中完成,然後重新啓用它。 我知道element.disabled = true和element.disabled = false(在函數之後這樣做)將不會使其回到啓用狀態,因爲我使用onkeydown啓用該函數,代碼有點像這樣 - >使用JavaScript禁用輸入文本的方法,直到一個功能完成

HTML:

<input type=text id=text1 onkeydown=myfunction(event)/> 

的Javascript:

function myfunction(event) { 
    var EnterKeyPressed = verifyKeyPressed(event, 13); 
    if (EnterKeyPressed == true) { 
    //here is where i want to disable the text box 
    fade(someotherOtherElement); 
    //i want to re enable it here 
    } 
} 

基本上它只是用的onkeydown一個文本框,當你通常鍵入它一直在進行,但是當你按回車鍵(鍵代碼= 13),它驗證與JavaScript事件,如果是true,subm其淡入淡出後的文本框的值。我希望在淡入淡出的情況下禁用文本條目,因爲在第一篇文章之後很快發佈其他內容時會導致部分淡入淡出。在element.disabled = TRUE後褪色(),elem.disaled =虛假不工作

回答

2

使用它的ID:

document.getElementById('text1').disabled = true; 
document.getElementById('text1').disabled = false; 

或者,給你的輸入字段的名稱,並確保它在形式(也有一個名稱):

document.myForm.myField.disabled=true; 
document.myForm.myField.disabled=false; 
+0

我試過,不工作,我認爲它的,因爲一旦你禁用它的onkeydown功能,其中淡入是無效的,因爲它的殘疾,所以你不能重新啓用它本身,並需要啓用另一個啓用的元素。感謝您的幫助 – 2011-06-11 03:59:13

+1

呃......好吧......它與您所說的「非常感謝」只是沒有jQuery相同的代碼。 – Dave 2011-06-11 15:39:29

+1

+1爲您的答案和評論兄弟。太糟糕了,你的答案沒有被標記爲選定的答案。給小孩一個JS圖書館,他會拍攝他胖乎乎的頭。 – thedethfox 2013-02-27 13:27:56

1

如果您已經使用jQuery,存在使用.attr和.removeattr API的一個很好的和容易的選擇,我用它來禁用這樣一個提交按鈕。

$('#itemAdd').submit(function(){ 
     $('input[type=text]', this).attr('disabled', 'disabled'); 
    }); 

要再次啓用該元素可以使用

$('#itemAdd').submit(function(){ 
     $('input[type=text]', this).removeAttr('disabled', 'disabled'); 
    }); 

API代碼可以在這裏對.attr和這裏.removiceattr

+0

謝謝,作品 – 2011-06-11 03:59:22

0

下面是使用jQuery的解決方案:

http://jsfiddle.net/pxfunc/8te6c/

HTML:

<input type="text" id="text1" /> 

的jQuery:

$("#text1").keydown(function(e) { 
    var EnterKeyPressed = verifyKeyPressed(e, 13); 
    if (EnterKeyPressed === true) { 
     //here is where i want to disable the text box 
     var that = this; 
     that.disabled = true; 

     $('#fadeMe').fadeOut(500, function() { // fadeOut's callback function 
      //i want to re enable it here 
      that.disabled = false; 
     }); 
    } 
}); 
+0

感謝很多mdmullinax jquery真是太棒了! – 2011-06-11 03:59:59

0

試試這個:

document.getElementById("text1").setAttribute("disabled", false); 

它成功地

0

工作對我來說,你說

elem.disaled = false 

不起作用

試試這個:

document.getElementById("elem")..removeAttribute('disabled'); 

它的工作對我來說

相關問題