简体   繁体   English

jQuery 显示表单文本输入

[英]jQuery Display Form Text Input

I'm pretty new to jQuery so I'm a little confused on how to make this work.我对 jQuery 很陌生,所以我对如何进行这项工作感到有些困惑。 Basically I have a form that has input fields such as Student ID, Student Name, Student DOB, and Student Age.基本上,我有一个表单,其中包含输入字段,例如学生 ID、学生姓名、学生出生日期和学生年龄。 Once the user has input their information into the fields, I want the input to show below the form in the format:一旦用户将他们的信息输入到字段中,我希望输入以以下格式显示在表单下方
Student ID: (input value)学生证:(输入值)
Student Name: (input value), etc.学生姓名:(输入值)等

This is my code so far:到目前为止,这是我的代码:

 $(document).ready(function() { $("#submitButton").on("click", function(e) { e.preventDefault(); var input = $("#q1").val() $("#show").html(input) }) $("#submitButton").on("click", function(e) { e.preventDefault(); var input = $("#q2").val() $("#show").html(input) }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <table> <tr> <label>Student Id <input id="q1" type="text" placeholder="required"/>*</label><br> <label>Student Name <input id="q2" type="text" placeholder="required"/>*</label><br> <label>Student DOB <input id="q3" type="datetime" placeholder="required"/>*</label><br> <label>Student Age <input id="q4" type="number" placeholder="required"/>*</label><br> </tr> </table> <button type="button" id="submitButton" class="button">Login</button> <span id="show"></span>

As of right now, when you press "submit" only the second value entered is shown, not the first.截至目前,当您按“提交”时,只显示输入的第二个值,而不是第一个。 I eventually need to have all fields showing on the bottom, but the second value is currently on the left side of the form.我最终需要在底部显示所有字段,但第二个值当前位于表单的左侧。 Any help/guidance is appreciated!任何帮助/指导表示赞赏!

You just need a single handler for the click, and use that one function to put together the string you desire from both values.您只需要一个用于单击的处理程序,然后使用该函数将您想要的两个值中的字符串组合在一起。 Here's one way of writing it:这是一种写法:

$(document).ready(function() {
  $("#submitButton").on("click", function(e) {
    e.preventDefault();
    var studentId = $("#q1").val();
    var studentName = $("#q2").val();
    $("#show").html(`Student ID: ${studentId}, Student Name: ${studentName}`);
  });
});

Note that the construct in that last line, with the backticks and ${...} syntax, is a template literal , which will not work if you have to run this on really old browsers like IE.请注意,最后一行中的构造,带有反引号和${...}语法,是一个模板文字,如果您必须在像 IE 这样的老式浏览器上运行它,它将不起作用。 (You really should't have to do that in this day and age, but since you're still using var to declare variables I feel I might have to cover this case.) In that case you can easily recreate the same string, it'll just be a little less nice to read, as "Student ID: " + studentId + ", Student Name: " + studentName . (在当今时代,您确实不必这样做,但是由于您仍在使用var来声明变量,我觉得我可能必须涵盖这种情况。)在这种情况下,您可以轻松地重新创建相同的字符串,它读起来会有点不那么好读,因为"Student ID: " + studentId + ", Student Name: " + studentName

I set the span as a div, and I just loop through the input elements and grab the label text and input value.我将跨度设置为 div,我只是遍历input元素并获取标签文本和输入值。 You also only need a single click handler.您还只需要一个单击处理程序。

 $("#submitButton").click(function(){ output = ""; $("input").each(function(){ output += $(this).parent("label").text().replace("*","") + ": " + $(this).val() + "<br>"; }); $("#show").html(output); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <table> <tr> <label>Student Id <input id="q1" type="text" placeholder="required"/>*</label><br> <label>Student Name <input id="q2" type="text" placeholder="required"/>*</label><br> <label>Student DOB <input id="q3" type="datetime" placeholder="required"/>*</label><br> <label>Student Age <input id="q4" type="number" placeholder="required"/>*</label><br> </tr> </table> <button type="button" id="submitButton" class="button">Login</button> <div id="show"></div>

What you are doing is creating two separate event handlers for the form submit, one for the id and one for the name.您正在做的是为表单提交创建两个单独的事件处理程序,一个用于 id,另一个用于名称。 So, on form submit, two functions will be called and step on each other's feet, one of them canceling the action of the other.因此,在表单提交时,将调用两个函数并相互踩踏,其中一个取消另一个的动作。

You should group your functionality into one event handler.您应该将您的功能组合到一个事件处理程序中。

Now, a clean way I'd highly recommend to get the form input values is to assign names to the inputs, and use the form's onsubmit event , eg for the age:现在,我强烈建议获取表单输入值的一种干净方法是为输入分配名称,并使用form's onsubmit事件,例如年龄:

<input id="q4" type="number" placeholder="required" name="age"/>

And then, inside the form's onsubmit event handler, the form is accessible by the keyword this as I explain in the snippet's comments.然后,在表单的onsubmit事件处理程序中,可以通过关键字this访问表单,正如我在代码段的注释中所解释的那样。 This way, the entered age can be accessed via this.age.value .这样,输入的年龄可以通过this.age.value访问。 Pretty neat!漂亮整齐!

Putting things together:把东西放在一起:

 $("#form").on("submit", function(e) { //prevent form submit from refreshing the page or redirecting e.preventDefault(); //get form input values //inside an event handler, `this` points to the element //to which the event handler is attached (in this case the form) //dispplay results in span $("#show").html(`Name: ${this.name.value}, DOB: ${this.dob.value}, Age: ${this.age.value}`); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <table> <tr> <label>Student Id <input id="q1" type="text" placeholder="required"/>*</label><br> <label>Student Name <input id="q2" type="text" placeholder="required" name="name"/>*</label><br> <label>Student DOB <input id="q3" type="datetime" placeholder="required" name="dob"/>*</label><br> <label>Student Age <input id="q4" type="number" placeholder="required" name="age"/>*</label><br> </tr> </table> <button type="submit" id="submitButton" class="button">Login</button> <span id="show"></span>

Problem is resolved问题已解决

 $( document ).ready(function() { $("#submitButton").on("click", function(e) { e.preventDefault(); var input = 'Std ID - '+$("#q1").val() +'</br> Std ID - '+$("#q2").val() +'</br> Std ID - '+$("#q3").val() +'</br>Std ID - '+$("#q4").val() +'</br>'; $("#show").html(input); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <div>Table</div> <table> <tr> <label>Student Id <input id="q1" type="text" placeholder="required"/>*</label><br> <label>Student Name <input id="q2" type="text" placeholder="required"/>*</label><br> <label>Student DOB <input id="q3" type="datetime" placeholder="required"/>*</label><br> <label>Student Age <input id="q4" type="number" placeholder="required"/>*</label><br> </tr> </table> <button type="button" id="submitButton" class="button">Login</button> <br><br> <span id="show"></span>

it's so easy to do with just javascript (and a little css - the real hard part)仅使用 javascript(和一点 css - 真正困难的部分)就很容易做到

In a form, the names attributes are used to directly identify each elements (no need to put IDs)在一个表单中,names属性用于直接标识每个元素(不需要放ID)

you also have to use the submit event by the <form> itself (not the button)您还必须通过<form>本身(而不是按钮)使用submit事件

 const myForm = document.getElementById('my-form') , showDiv = document.getElementById('show-div') ; myForm.onsubmit = e => { e.preventDefault() showDiv.innerHTML = `Student Id..: ${myForm.Student_Id.value} <br> Student Name: ${myForm.Student_Name.value} <br> Student DOB.: ${myForm.Student_DOB.value} <br> Student Age.: ${myForm.Student_Age.value} ` } // suggestion for init myForm.Student_DOB.valueAsDate = new Date() // today! myForm.Student_Age.valueAsNumber = 20 // less or more...
 label { width: 20em; display: block; float: left; clear: both; line-height: 2.7em; } label:before { content: '*'; float: right; } input { float: right; margin-right: .4em; width: 12em; height: 1.2em; margin: .3em; } button { display: block; float: left; clear: both; margin-top: 1em; } #show-div { float: left; clear: both; margin: 1em; font-family: monospace; }
 <form id="my-form"> <label> Student Id <input name="Student_Id" type="text" placeholder="required"/> </label> <label> Student Name <input name="Student_Name" type="text" placeholder="required"/> </label> <label> Student DOB <input name="Student_DOB" type="date" placeholder="required"/> </label> <label> Student Age <input name="Student_Age" type="number" placeholder="required"/> </label> <button type="submit">Login</button> </form> <div id="show-div"></div>

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

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