如何通过JS获取表单中输入的内容?

时间:2016-01-02 19:39:37

标签: javascript jquery html forms variables

我有一个代码位,我正在制作表格。有一个数字输入,我想知道如何获取该输入并将其存储在变量中以进行JS计算。如何在提交时获取输入中的当前值并使其成为变量?

$(Document).ready(function() {
  $("input[type='submit']").click(function() {

  });
});
body {
  background-color: skyblue;
}
<!DOCTYPE html>
<html>

<head>
  <link rel='stylesheet' href='style.css' />
  <script src='script.js'></script>
  <meta charset="UTF-8">
  <title>Classified</title>
</head>

<body>
  <h1>Classified</h1>
  <form action="script.js" name="classified" method="get">
    <input type="number" name="classified" min="0">
    <input type="submit" value="submit">
  </form>
  <h3 hidden>Classified</h3>
  <h3 hidden>Classified</h3>
</body>

</html>

3 个答案:

答案 0 :(得分:2)

import pickle

def user():
    name_input = raw_input("Name: ")
    age_input = raw_input("Age: ")
    email_input = raw_input("E-Mail: ")
    ph_input = raw_input("Phone Number: ")
    user_input = {'Name': name_input, 'Age': age_input, 'E-Mail': email_input, 'Phone number': ph_input}
    return user_input

a = user()
with open("picke.txt", "wb") as f:
    pickle.dump(a,f, pickle.HIGHEST_PROTOCOL)

b = "abc"
with open("picke.txt", "wb") as fi:
    pickle.dump(b,fi, pickle.HIGHEST_PROTOCOL)

答案 1 :(得分:1)

试试这个:

var inputField = $("input[type='number']");

答案 2 :(得分:1)

如果您允许更改表单的标记,我建议给它一个ID,例如

$(document).ready(function() {
    $(".crousel-set").each(function(index, crousel) {
        crousel.owlCarousel();
    });
});

然后在你的JS中

<form action="script.js" name="classified" method="get">
    <input id="my-id" type="number" name="classified" min="0">
    <input type="submit" value="submit">
</form>

jQueries .val()方法返回表单元素的$(document).ready(function() { $("input[type='submit']").click(function() { var myNumVal = $('#my-id').val(); }); }); 属性。 此外,正如您的帖子下面的评论中所述 - 我也相信value区分大小写,应该Document至少是您的代码实际运行的安全。