javascript onClick if / else什么都不做

时间:2016-07-02 22:07:23

标签: javascript onclick

我正在尝试javascript中的onClick函数,但似乎什么也没做。

 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

2 个答案:

答案 0 :(得分:0)

尝试使用以下语法

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

答案 1 :(得分:0)

这有助于您:

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