The difference between the. Val () and. attr ("value") of jquery from the Reset input File tab

Source: Internet
Author: User

Background:

When the input file tag is emptied , the following methods are used to find the correct error:

        "√" $ ("#file") [0].value = "";        "√" $ ("#file") [0].value = null;        "x" $ ("#file"). attr ("Value", "");        "x" $ ("#file"). attr ("value", null);                "√" $ ("#file"). Val ("");        "√" $ ("#file"). val (null);

Why is it that the same value is changed, and the results are different?

Explanation:

Check out StackOverflow jQuery val () vs. attr ("value") Question, which are answered as follows:

The gist is, which is .attr(...) , only getting, the objects value at the start (when the HTML is created). is val() getting the obj ECT ' s property value which can change many times.

The translation comes from:

. Val () Sets the Value property of input, input is an instance of Htmlinputelement, and value is defined by the setter method, when it is assigned, the value is written to input, and the method of changing the Value property, The DOM's Value property is actually manipulated, triggering the browser's repaint to update the value of input.

Resources:

forms-jquery val () vs. attr ("value")-Stack Overflow

See the difference between. Val () and. attr ("value") of jquery from the Reset input File tab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.