How to use ajax to operate a form with JavaScript

php中世界最好的语言
Release: 2018-03-31 13:40:28
Original
1771 people have browsed it

This time I will bring youJavaScriptHow to use ajax to operate the form, what are theprecautionsfor using ajax to operate the form with JavaScript, the following is a practical case, let's take a look.

Using JavaScript to operate forms is similar to operating DOM, because the form itself is also a DOM tree.

However, the input box, drop-down box, etc. of the form can receive user input, so using JavaScript to operate the form can obtain the content entered by the user, or set new content for an input box.

The input controls of HTML formmainly include the following types:

  • Text box , the correspondingis used to enter text;

  • Password box, the correspondingis used to Enter the password;

  • The radio button, corresponding, is used to select one item;

  • Check box, corresponding, used to select multiple items;

  • Drop-down box, corresponding < ;select>, used to select an item;

  • Hidden text, corresponding, is not visible to the user, but will be hidden when the form is submitted Text is sent to the server.

##Get the value

If we get a reference to an node, we can call value directly Get the corresponding user input value:

//  var input = document.getElementById('email'); input.value; // '用户输入的值'
Copy after login
This method can be applied to text, password, hidden and select. However, for radio buttons and check boxes, the value attribute always returns the preset value of HTML, and what we need to get is actually whether the user has "checked" the option, so we should use checked to judge:

//  //  var mon = document.getElementById('monday'); var tue = document.getElementById('tuesday'); mon.value; // '1' tue.value; // '2' mon.checked; // true或者false tue.checked; // true或者false
Copy after login

Set the value

Setting the value is similar to getting the value. For text, password, hidden and select, just set the value directly:

//  var input = document.getElementById('email'); input.value = 'test@example.com'; // 文本框的内容已更新
Copy after login
For radio buttons and check boxes, just set checked to true or false.

HTML5 controls

HTML5 has added a large number of standard controls, commonly used ones include date, datetime, datetime-local, color, etc., they all Use the tag:

  
Copy after login
Browsers that do not support HTML5 cannot recognize the new controls and will display them as type="text". Browsers that support HTML5 will get the formatted string. For example, the value of an input of type="date" is guaranteed to be a valid date in YYYY-MM-DD format, or an empty string.

Submit the form

Finally, JavaScript can handle form submission in two ways (the AJAX method will be introduced later).

Method one is to submit a form through the submit() method of the
element. For example, in response to a
Copy after login
The disadvantage of this method is that it disrupts the browser's normal submission of the form. By default, the browser submits the form when clicking
Copy after login
Note that you must return true to tell the browser to continue submitting. If you return false, the browser will not The form will continue to be submitted. This situation usually corresponds to the user input error, prompting the user

error messageand then terminating the submission of the form.

When checking and modifying, make full use ofto pass data.

For example, many login forms expect users to enter their username and password. However, for security reasons, the plaintext password is not transmitted when submitting the form, but the MD5 of the password. Ordinary JavaScript developers will directly modify:

 
Copy after login
This approach seems fine, but when the user enters the password and submits it, the display of the password box will suddenly change from a few * to 32 * (Because MD5 has 32 characters).

要想不改变用户的输入,可以利用实现:

 
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

用history让ajax支持前进/后退/刷新

原生ajax与封装的ajax使用方法(附代码)

The above is the detailed content of How to use ajax to operate a form with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Previous article:Detailed explanation of the use of AJAX and JavaScript Next article:Ajax to implement infinite loading of lists and secondary drop-down menu options (with code)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!