2016-10-05 104 views
0

我想知道我是如何使用另一個功能禁用一個功能的。我有一個表格要求額外的課程作業,但如果用戶沒有完成任何額外工作,則隱藏輸入。如果他們刪除了一組輸入並改變了他們的想法,我想進行失敗保護。使用另一個功能禁用一個功能 - 香草JS

我需要幫助的是編寫故障安全功能,檢查:如果功能1發生,禁用它。

CodePen:http://codepen.io/theodore_steiner/pen/ORzpQQ

function hideCourseWork() { 
 
    var otherEducationHistory = document.getElementById("otherEducationHistory"); 
 
    otherEducationHistory.style.display = "none"; 
 

 
    if (otherEducationHistory.style.display == "none") { 
 
    var noAdditionalCourseWork = document.getElementById("noAdditionalCourseWork"); 
 

 
    var checkNo = document.getElementById("checkNo"); 
 

 
    var undo = document.getElementById("undoNoAdditionalCourseWork"); 
 

 
    noAdditionalCourseWork.style.top = "-48px"; 
 
    checkNo.style.top = "-65px"; 
 
    undo.style.top = "-65px"; 
 
    undo.style.top = "-63px"; 
 
    } 
 

 
};
<div class="input-group" id="other-education"> 
 
    <p class="subtitleDirection">Please list in chronological order, any additional cources beyond the degree(s) listed above, including any Ministry of Education Courses. If you have not completed any additional course work, please indicate.</p> 
 
    <div class="clearFix"></div> 
 
    <label id="otherEducation">Additional Courses*</label> 
 
    <div id="otherEducationHistory"> 
 
    <input type="text" class="three-lines" name="otherUniversity_1" placeholder="Institution" onblur="this.placeholder='Institution'" onfocus="this.placeholder=''" /> 
 
    <input type="text" class="three-lines" name="otherDegree_1" placeholder="Degree/Diploma" onblur="this.placeholder='Degree/Diploma'" /> 
 
    <input type="date" class="three-lines" name="otherEducationYear_1" /> 
 
    <input type="button" value="+" onclick=addOtherEducation() /> 
 
    </div> 
 
    <!--end of otherEducationHistory div--> 
 

 
    <div class="clearFix"></div> 
 
</div> 
 
<!--end of other-education div--> 
 

 
<p id="noAdditionalCourseWork">I have not completed any additional course work.</p> 
 
<input type="radio" name="checkNo" id="checkNo" onclick="hideCourseWork()" /> 
 
<br /> 
 
<p id="undoNoAdditionalCourseWork" onclick="reAddCourseWork()">(Undo).</p>

+0

我真的困惑的問題是什麼嗎?我們是不是隻是隱藏和顯示基於無線電價值的HTML元素? –

+0

是的。然而,我想更進一步,並學習如何通過函數來​​實現它......僅僅爲了這個例子以及未來的知識。 –

+0

爲什麼過度設計工作?我從來沒有在我的整個職業生涯中'禁用'一個功能 –

回答

1

你不會被你的問題真正清楚,但也許這將幫助。

函數與變量的排序方式相同。它們可以被重新分配,(就像一個空的函數,例如)..

function doSomething(){ 
    alert("didSomething"); 
} 

function disableDoSomething(){ 
    window.doSomething = function(){}; 
} 

如果您希望能夠重新啓用該功能後,你可以做這樣的事情...

function doSomething(){ 
    alert("didSomething"); 
} 

var functionHolder; 
function disableDoSomething(){ 
    if(!functionHolder) functionHolder = window.doSomething; 
    window.doSomething = function(){}; 
} 

function enableDoSomething(){ 
    window.doSomething = functionHolder; 
} 
+0

讓我看看我是否理解你,在我的例子中,doSomething是最初的功能。但是在disableDoSomething中你已經把function(){};禁用代碼應該放在那裏?我不明白如何去實際禁用該功能。 –

+0

只是用您的實際功能替換'doSomething'。空的功能應該是空的。這個答案實際上與上面的答案完全相同,唯一的區別是函數的定義方式。都使用將函數分配給空函數的相同概念。 –

+0

有道理。感謝您的解釋。 –

1
var foo =() => { /* foo definition */ }, 
    bar = foo; 

/* re-assign foo to a new, empty, function implementation; 
    effectively "disabling" it */ 
foo =() => {}; 

/* ... code that uses function reference foo ...*/ 

/* re-enable foo's original behavior by assigning bar to foo */ 
foo = bar; 

爲後人的緣故(ES5):

var foo = function() { /* foo definition */ }, 
    bar = foo; 

/* re-assign foo to a new, empty, function implementation; 
    effectively "disabling" it */ 
foo = function(){}; 

/* ... code that uses function reference foo ...*/ 

/* re-enable foo's original behavior by assigning bar to foo */ 
foo = bar; 
+1

這是偉大的,如果你使用最新的Firefox或鉻,其他人將成爲SOL –