2012-07-30 52 views
0

措辭這個問題很難,但我在這裏。好吧,我使用的代碼從該網站: http://www.switchonthecode.com/tutorials/javascript-tutorial-getting-user-input-with-prompt-and-confirmJavascript通過操作確認嗎?

<script type="text/javascript"> 
function confirmInput() 
{ 
    var ans = confirm("Install a virus and delete all your files?"); 

    alert(ans ? document.getElementById('level').value = "0"; 
       : "If no, do nothing"); 
} 
</script> 

<input type="button" onclick="confirmInput()" value="Show me the Question!" /> 

我甚至試過,以取代用行動回答的內容,但我什麼也得不到。

我如何爲答案添加一個動作,所以當它是肯定的時候,我做了一些事情,當沒有時我不做。

+2

適用於我:http://jsfiddle.net/WTsMe/ – 2012-07-30 20:51:06

+0

對不起,我只是更新了它...我很着急 – 2012-07-30 20:51:15

+1

定義「不工作」。如果這就是你的意思,你不會從確認中返回價值。 – 2012-07-30 20:51:34

回答

2

我認爲這是你在找什麼:

function confirmInput() 
{ 
    if(confirm("Install a virus and delete all your files?")) 
     document.getElementById('level').value = "0"; 
} 
+0

完美!謝謝 – 2012-07-30 20:55:53

0

有在你的代碼在這裏語法錯誤:

alert(ans ? document.getElementById('level').value = "0"; // this semicolon is invalid 
      : "If no, do nothing"); 

它執行,直到這個分號終止。檢查你的控制檯,應該有錯誤信息。

另一件事是?:操作符只能返回一個值。它不應該包含操作,只包含返回值的值或函數。

+0

好的,謝謝...... – 2012-07-31 03:56:12