2016-10-05 18 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(){}; 
} 

後で再度有効に機能することができるようにしたい場合は、あなたがこのような何かを行うことができ...

後世の日本酒(ES5)のために
function doSomething(){ 
    alert("didSomething"); 
} 

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

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

私があなたのことを理解しているかどうかを見てみましょう。私の例では、doSomethingが最初の関数になります。しかし、disableDoSomething関数(){};無効になっているコードはそこに入るはずですか?私は実際に機能を無効にする方法について理解していません。 –

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

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やChromeを使用している場合、他の誰かがSOLになる –

関連する問題