javascript按钮什么都不做onclick

时间:2014-08-03 08:23:17

标签: javascript onclick spinner form-submit

首先,我不是很擅长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>

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

3 个答案:

答案 0 :(得分:1)

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

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

function check(result)  {
  ...
}

在您的情况下result定义在gennum内,check无法访问它。

答案 1 :(得分:0)

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
}

答案 2 :(得分:0)

您可以在函数之外声明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 ...
}