2016-07-02 4 views
-1

Ich versuche die onClick-Funktion in Javascript, aber es scheint nichts zu tun.Javascript onClick if/else macht nichts

a.button(onClick=function() { 
    answer=prompt('To ERASE ALL DATA, please type YES and click OK.'); 
    if (answer == "YES") { 
      answer=promptPopup("Would you like to keep the system IP? Please type YES to keep these settings or NO to erase them and click OK."); 
      if (answer == null) { 
       alert("No action taken because Cancel was clicked."); 
      } else if (answer.toLowerCase() == "yes") { 
       location.href="/html/xxx"; 
      } else if (answer.toLowerCase() == "no") { 
       location.href="/html/yyy"; 
      } else { 
       alert("No action taken. Please answer either YES or NO to perform the action or click Cancel to quit."); 
      } 
     } else if (answer != null) { 
      alert("No action taken. Please either type YES to perform the action or click Cancel to quit."); 
     } 
    }) 
    | Reinitialize System 
+0

Ähm ... das ist nicht, wie Sie ein Ereignis-Listener registrieren ... Es sieht wie Python-Syntax aus, vielleicht bist du es von einem Python-Hintergrund? –

+0

Dies ist Javascript-Code, den ich von meiner Jade-Vorlage aus anrufe. Jede Hilfe würde geschätzt werden, wie ich das erreichen kann. – Rsk23

+0

Wenn Sie mit JS/HTML nicht vertraut sind, werfen Sie einen Blick auf [diese Seite] (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). –

Antwort

0

Try folgende Syntax:

<input value="ClickMe" onclick="alert('Click!')" type="button"> 
0

Diese Hilfe Sie:

<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
    </head> 
 
    <body> 
 
     <button id="btn">Click</button> 
 
     <script> 
 
      var btn = document.getElementById("btn"); 
 
        btn.onclick = function() { 
 
        var answer=prompt('To ERASE ALL DATA, please type YES and click OK.'); 
 
        if (answer == "YES") { 
 
        answer=promptPopup("Would you like to keep the system IP? Please type YES to keep these settings or NO to erase them and click OK."); 
 
        if (answer == null) { 
 
        alert("No action taken because Cancel was clicked."); 
 
        } else if (answer.toLowerCase() == "yes") { 
 
        location.href="/html/xxx"; 
 
        } else if (answer.toLowerCase() == "no") { 
 
         location.href="/html/yyy"; 
 
        } else { 
 
         alert("No action taken. Please answer either YES or NO to perform the action or click Cancel to quit."); 
 
        } 
 
        } else if (answer != null) { 
 
        alert("No action taken. Please either type YES to perform the action or click Cancel to quit."); 
 
        } 
 
       } 
 
       
 
     </script> 
 
    </body> 
 
</html>