Hidden field of input form in JS and its application

  • 2021-07-18 07:10:14
  • OfStack

1. Form Hidden Fields

Hidden fields are invisible elements that are used to collect or send information. Hidden fields are invisible to visitors to web pages. When the form is submitted, the hidden field sends the information to the server with the name and value you defined when you set it up.

Code format:


 < input type="hidden" name="..." value="..." > 

Attribute interpretation:

type= "hidden" defines hidden fields;

name attribute defines the name of the hidden field. To ensure the accurate collection of data, it is necessary to define a name with one only and no two;

The value attribute defines the value of the hidden field

For example:


 < input type= " hidden "  name= " ExPws "  value= " dd "> 

In fact, to put it bluntly, the hidden field is not visible in the foreground, just like the element 1 of the form. There are names and values, but the submitted data is invisible

2. Use of hidden fields

The use of hidden field is to take the value ID value or other variable value from the table, but it is not displayed in the page, and then submit it to a new page, and take out the value through request. form ("name for hidden domain name").

For example:

1 、<input type=hidden name=coun value=<%=cc%> The hidden domain name here is coun with a value of <%=cc%> If the previous cc=100, the value is 100;

2. Submit the form to the new page xx. asp;;

3. In the xx. asp page, use the request.write request.form(“coun”) The value displayed on the page is 100

Simply put, if you want to extract some information from the previous page, but you can't display it on the previous page, you will use hidden fields

3. The role of hidden fields

1 Hidden fields are invisible to the user in the page. The purpose of inserting hidden fields in the form is to collect or send information for use by programs processing the form. When the viewer clicks the Send button to send the form, the information of the hidden field is also sent to the server.

2 Sometimes we have to give the user 1 information to submit to confirm the user's identity when submitting the form, such as sessionkey, etc. Of course, these things can also be implemented with cookie, but it is much easier to use hidden fields. And there is no trouble that the browser does not support it and the user disables cookie.

3 Sometimes there are multiple submit buttons in an form. How can the program distinguish clearly which button the user pressed to submit? We can write a hidden field, and then add it at every button onclick=”document.form.command.value=”xx”“ Then we will check the value of command after receiving the data, and we will know which button the user pressed to submit it.

4 Sometimes there are multiple form in a web page. We know that multiple form cannot be submitted at the same time, but sometimes these form do interact, so we can add hidden fields in form to connect them.

5 javascript does not support global variables, but sometimes we have to use global variables, so that we can put the value in the hidden field first, and its value will not be lost.

6 There is another example, such as press a button pop-up 4 small windows, when clicking on one of the small window when the other 3 automatically closed. But IE does not support small windows to call each other, so only in the parent window write a hidden field, when the small window see that hidden field value is close on their own closed.


Related articles: