Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between attr and val in jquery

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you what are the relevant knowledge points about the difference between attr and val in jquery. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Differences: 1, the val method can obtain the value entered manually, while the attr method cannot; 2, if you use val for assignment, you can use the val method to obtain the value, while the attr method cannot obtain it; 3, when using attr assignment, if you manually change the input value, val can get the latest value, while attr obtains the original value.

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What is the difference between attr and val in jquery

Let's talk about the conclusion first and then paste the code analysis. The difference is as follows:

1.val can get the value entered manually, but attr cannot

two。 With val assignment, val can get the value, but attr cannot.

3. With attr assignment, both val and attr can get the value. If you change the input value manually, val can get the latest value. What attr reads is the value assigned by attr at the beginning.

Enter a number manually first

Then print it out with val () and attr ("value"), respectively.

Console.log ("this is val:" + $("# jibengongzi"). Val (); console.log ("this is attr:" + $("# jibengongzi"). Attr ("value"))

As you can see, attr ("value") cannot get the value entered manually

Next, use val () to assign the value (ruleForm.jibengongzi is a number)

$("# jibengongzi") .val (ruleForm.jibengongzi)

And print it out.

Then use attr to assign values

("# jibengongzi") .attr ("value", ruleForm.jibengongzi)

These are all the contents of the article "what's the difference between attr and val in jquery". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report