弹出框值

时间:2013-03-05 12:09:11

标签: javascript html

如何将表值放在弹出框中?它输出“undefined”

而不是选定的表值
<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.+"?");

之后的下一步

1 个答案:

答案 0 :(得分:2)

您继续在每个r块中重新声明if变量,从而有效地使confirm()返回的值永远不会到达您读取的行。

尝试一劳永逸地声明r

var x, res, y, r;

并将var关键字放在您为r指定值的行上。