【问题标题】:javascript Button Doing Nothing onclickjavascript 按钮无操作 onclick
【发布时间】:2014-08-03 16:37:29
【问题描述】:

首先,我不是很擅长 javascript。好吧,我想做的是创建一种轮子旋转程序,在该程序中,单击按钮,您将获得额外的旋转,什么也没有,或者它会向我发送一封带有密码的电子邮件,我将在电子邮件中回复如何他们会赢很多。希望您理解我的意思并知道它为什么不起作用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Spin the Wheel!</title>
</head>
<body>
<button name="generate" type="button" onClick="gennum()">Spin!</button>

<script>
function gennum() {
var result=Math.floor(Math.random()*101)
check()
}

function check()  {
if (result > 0 && result < 11) {
alert("You win 1 free spin! Spin again!")
}
else if (result > 10 && result < 16) {
alert("You win 2 free spins! Spin again twice!")
}
else if (result > 15 && result < 26) {
alert("Sorry, you won nothing. Please try again tommorow.")
}
else if (result > 25 && result < 36) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('7L6XPaY8').value)
    ;

    window.location.href = link;
}
else if (result > 35 && result < 45) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('NmE6B5uF').value)
    ;

    window.location.href = link;
}
else if (result > 44 && result < 52) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('ZpLb9TRC').value)
    ;

    window.location.href = link;
}
else if (result > 51 && result < 59) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('JQa6fGHH').value)
    ;

    window.location.href = link;
}
else if (result > 58 && result < 64) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('rKjPGXak').value)
    ;

    window.location.href = link;
}
else if (result > 63 && result < 69) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('5QQyCJaD').value)
    ;

    window.location.href = link;
}
else if (result > 68 && result < 71) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('474zbnkE').value)
    ;

    window.location.href = link;
}
else if (result > 70 && result < 74) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('QUjY2NSN').value)
    ;

    window.location.href = link;
}
else if (result > 73 && result < 76) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('FNVYSHu5').value)
    ;

    window.location.href = link;
}
else if (result > 75 && result < 100) {
alert("Sorry, you won nothing. Please try again tommorow.")
}
else if (result = 100) {
    var link = "mailto:name@example.com"
             + "&subject=" + escape("Spinner")
             + "&body=" + escape(document.getElementById('uZ63V4me').value)
    ;

    window.location.href = link;
}
}
</script>

</body>
</html>

这就是所有的代码。如果您知道它为什么不起作用,请回复。

【问题讨论】:

    标签: javascript onclick spinner form-submit


    【解决方案1】:

    您必须将result 传递给函数,否则它超出范围:

    function gennum() {
      var result=Math.floor(Math.random()*101)
      check(result)
    }
    
    function check(result)  {
      ...
    }
    

    在您的情况下,result 是在 gennum 内部定义的,而 check 无权访问它。

    【讨论】:

    • 感谢您的回答,我忘了这样做。
    【解决方案2】:

    result 未声明为global 变量,因此到达check() 时为空。

    将其作为parameter 传递给check()

    function gennum() {
     var result=Math.floor(Math.random()*101)
     check(result);
    }
    
    function check(param)  {
     // and so on and so forth
    }
    

    【讨论】:

      【解决方案3】:

      您可以在函数外部声明result 变量,以通过所有方法公开访问它

      var result;
      function gennum() {
        result=Math.floor(Math.random()*101);
        check();
      }
      function check()  {
      //do something here ...
      }
      


      您应该为check 函数声明一个参数并将值传递给它。

      function gennum() {
        var result=Math.floor(Math.random()*101);
        check(result);
      }
      function check(result)  {
      //do something here ...
      }
      

      【讨论】:

        猜你喜欢
        • 2015-09-01
        • 2019-06-13
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        相关资源
        最近更新 更多