-4

如何将表格值放入弹出框中?它输出“未定义”而不是选定的表值

<script>
  function conf () {
    var x, res, y;
    res = document.getElementById('t').onclick
    if(document.formxml.num1.value >= 2) {
      var r = confirm("Reserve for " + document.formxml.num1.value + " persons in "+document.formxml.res. + "?");
    } else if(document.formxml.num1.value == 1) {
      var r = confirm("Reserve for " + document.formxml.num1.value + " person in " + document.formxml.res. + "?");
    } else if(document.formxml.num1.value < 0) {
      var r = confirm("You cannot input a negative number!");
    }

    if(r == true) {
      x = "";
      location.href = 'res3.html'
    } else if(r == false) {
      x = "";
    }
  }
</script>

</head>
<body>

  <style type="text/css">
    .txt {width:50px;}
  </style>

  <form action="" method="get" name="formxml">
    For how many persons?
    <input type="text" name="num1" value="" class="txt" id="num1" maxlength="2">
    <input type="button" value="Table 1" id="t" name="res" onclick="conf();">
    <input type="button" value="Table 3" id="t" name="res" onclick="conf();">
  </form>

我不知道接下来会发生什么var r=confirm("Reserve for "+document.formxml.num1.value+" person in "+document.formxml.res.+"?");

4

1 回答 1

2

你不断地r在每个if块中重新声明你的变量,实际上导致返回的值confirm()永远不会到达你读取它的行。

尝试r与其他变量一起一劳永逸地声明:

var x, res, y, r;

并将var关键字放在您为其赋值的行上r

于 2013-03-05T12:11:06.600 回答