简体   繁体   English

从HTML表单获取值

[英]Get values from HTML form

I'm trying to get the values from a form I've created in HTML: I'm using Javascript to try and get the values from the form, I've tried multiple ways of getting the values, at the moment it's this 我正在尝试从用HTML创建的表单中获取值:我正在使用Javascript尝试从表单中获取值,我尝试了多种获取值的方法,目前是这样

 $(document).ready(function() { $('#submit').click(function() { var x = document.getElementById("input"); var text = ""; var i; for (i = 0; i < x.length; i++) { console.log(x.elements[i].value); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="input"> First Name: <br/> <input type="text" name="firstName"/><br/> Last Name: <br/> <select id="lastName" style="text-align: center;"> </select><br/> <p>Date of Birth:</p><br /> Day: <br/> <select id="dayDropDown" style="text-align: center;"> </select><br/> Month: <br/> <select id="monthDropDown" style="text-align: center;"> </select><br/> Year: <br/> <select id="yearDropDown" style="text-align: center;"> </select><br/> Country: <br/> <input type="text" name="country"/><br/> </form> <br/> <button id="submit" type="button">Submit</button> 

This is meant to output the values from the form to my VS the output window, but I get nothing. 这是为了将表单中的值输出到我的VS输出窗口,但是我什么也没得到。 any ideas? 有任何想法吗?

As you're already using jQuery to attach your event handler, you may as well use it to select all the elements in the form using the :input selector. 因为您已经在使用jQuery来附加事件处理程序,所以最好使用:input选择器来使用它来选择表单中的所有元素。 From there you can simply loop through them all using each() and output their values to the console, like this: 从那里,您可以简单地使用each()遍历它们,并将其值输出到控制台,如下所示:

 $(document).ready(function() { $('#submit').click(function() { $('form :input').each(function() { console.log($(this).val()); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="input"> First Name: <br/> <input type="text" name="firstName"/><br/> Last Name: <br/> <select id="lastName" style="text-align: center;"> </select><br/> <p>Date of Birth:</p><br /> Day: <br/> <select id="dayDropDown" style="text-align: center;"> </select><br/> Month: <br/> <select id="monthDropDown" style="text-align: center;"> </select><br/> Year: <br/> <select id="yearDropDown" style="text-align: center;"> </select><br/> Country: <br/> <input type="text" name="country"/><br/> </form> <br/> <button id="submit" type="button">Submit</button> 

You can use jQuery and get the data with one statement 您可以使用jQuery并通过一条语句获取数据

var queryString = $('#input').serialize();

See this live example for a demo. 请参阅此实时示例以获取演示。

getElementsByTagName will solve your problem. getElementsByTagName将解决您的问题。

This is your code as modificated: 这是修改后的代码:

 $(document).ready(function () {
        $('#submit').click(function () {
            var x = document.getElementsByTagName("input");
            var text = "";
            var i;
            for (i = 0; i < x.length; i++) {
                console.log(x[i].value);
            }
        });
    });

Apply with same Id name with all input . 在所有输入中应用相同的Id名称。 your javascript was working ... 您的JavaScript工作正常...

 $(document).ready(function () { $('#submit').click(function () { var x = document.getElementById("input"); var text = ""; var i; for (i = 0; i < x.length; i++) { console.log(x.elements[i].value); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="input"> First Name: <br/> <input type="text" name="firstName" id="input"/><br/> Last Name: <br/> <select id="lastName" style="text-align: center;" id="input"> </select><br/> <p>Date of Birth:</p><br /> Day: <br/> <select id="dayDropDown" style="text-align: center;" id="input"> </select><br/> Month: <br/> <select id="monthDropDown" style="text-align: center;" id="input" > </select><br/> Year: <br/> <select id="yearDropDown" style="text-align: center;" id="input"> </select><br/> Country: <br/> <input type="text" name="country" id="input"/><br/> </form> <br/> <button id="submit" type="button">Submit</button> 

I'd suggest that you add the jquery-serialize-object reference,so it may do the things become a bit easier: 我建议您添加jquery-serialize-object引用,这样可能会使事情变得容易一些:

 $('#input').submit(function(){
   var objForm = $(this).serializeObject();
    console.log(objForm);
 });

Here you can find other examples and the jquery-serialize-object library for downloading 在这里您可以找到其他示例以及用于下载的jquery-serialize-object库

$(document).ready(function () {
    $('#submit').click(function () {
        $('form input, form select').each(function () {
            console.log($(this).val());
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM