Javascript通过行动确认?

时间:2012-07-30 20:48:57

标签: javascript html

写这个问题很难,但我走了。好的,我正在使用本网站的代码: http://www.switchonthecode.com/tutorials/javascript-tutorial-getting-user-input-with-prompt-and-confirm

<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 个答案:

答案 0 :(得分:2)

我认为这就是你要找的东西:

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

答案 1 :(得分:0)

您的代码中存在语法错误:

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

执行直到此分号并终止。检查你的控制台,应该有一条错误信息。

另一件事是?:运算符只能返回一个值。它不应该包含操作,只能包含返回值的值或函数。