简单的JS表格;找不到提交值

时间:2017-06-26 01:51:07

标签: javascript html forms

我是JS的新手,我在存储和查找表单提交的价值时遇到了问题。我能够创建一个简单的表单(如下所示),但我无法找到要存储的提交值以供以后使用。

我以为我能够在var number = document.getElementById('fib-form-number');访问for值,但我似乎做错了。

我查看了这篇文章here,但这似乎没有用。

我知道修复很容易......但我无法弄清楚。

谢谢,

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Form/title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="../static/js/jquery.min.js"></script>
        <script type="text/javascript" src="../static/js/index.js"></script>

    </head>
    <body>

        <!-- <input id="input" type="submit" value="Submit" onclick="return submitted();"> -->
        <!-- Use form to capture numbers on submission -->
        <form id="fib-form">
            <input type="number" id="fib-form-number" min="1">
            <button type="submit">Submit</button>
        </form>

    </body>
</html>

JS

// Get value of submission

$(document).ready(function() {

    var form = document.getElementById('fib-form');
    var number = document.getElementById('fib-form-number');

    // Now get the value of the submission
    form.onsubmit = function () {
        var variable = number.value;
        console.log(variable)
    }

});

1 个答案:

答案 0 :(得分:0)

你拼错了变量:

    var variable = number.value;
    console.log(varaible)

但是你也可以使用jQuery,因为你已经在使用它了:

$(document).ready(function() {
    var form = $('fib-form');
    var number = $('fib-form-number');

    // Now get the value of the submission
    form.onsubmit = function () {
        var variable = number.val();
        console.log(variable)
    }

});