简体   繁体   English

.prop()VS .val()。使用jQuery设置输入文本值

[英].prop() VS .val() .Setting an input text value with jQuery

After reading (the interesting) .prop() vs .attr() and jQuery Performance : attributes doubt arise in my mind about what is better to use: .prop() or .val() ? 阅读(有趣的) .prop()vs .attr()jQuery Performance:属性怀疑在我的脑海中出现了什么更好用:.prop()或.val()? I want to set an input text value. 我想设置一个输入文本值。

The jQuery .prop() page said the following: jQuery .prop()页面说了以下内容:

Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. 属性通常会影响DOM元素的动态状态,而不会更改序列化的HTML属性。 Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. 示例包括输入元素的value属性,输入和按钮的disabled属性或复选框的checked属性。 The .prop() method should be used to set disabled and checked instead of the .attr() method. 应该使用.prop()方法来设置disabled和checked而不是.attr()方法。 The .val() method should be used for getting and setting value . 应该使用.val()方法来获取和设置值

But regarding to performance .prop() seems to be better than .val() setting values: 但是关于性能.prop()似乎比.val()设置值更好: .prop()vs .attr()vs .val()

So I don't have an answer. 所以我没有答案。 What I should do? 我该做什么?

$('#element').prop('value', 'Example');

or 要么

$('#element').val('Example');

EDIT1: .prop() is always better than .val() EDIT1: .prop()总是优于.val() 在此输入图像描述在此输入图像描述在此输入图像描述

EDIT2: I've tried now getting a value , also .prop() is faster 编辑2:我现在尝试获取一个值 ,也.prop()更快 在此输入图像描述

EDIT3: As @BeatAlex says if we want performace native js is extremely faster EDIT3:正如@BeatAlex所说,如果我们想要性能本机js 非常快

var x = document.getElementById('element').value;

在此输入图像描述

As some of you said it may be faster, but if .val() exist we'd use it. 正如你们中的一些人说它可能会更快,但如果存在.val(),我们就会使用它。

PS: while I am writing this, current jQuery version is 1.11 or 2.1 PS:当我写这篇文章时,当前的jQuery版本是1.11或2.1

You want to use val() . 你想使用val() That's what the function is for, specifically for the val ue of the input. 这就是功能是干什么用的,专门为val输入UE。

It's also quicker to write, easier to understand and while using val , you can change the value multiple times if need be. 写入也更快,更容易理解,使用val ,如果需要,可以多次更改值。

Your chart says 你的图表说

Higher is better 越高越好

and val() higher. val()更高。

Also, like you've already quoted, 另外,就像你已经引用的那样,

The .val() method should be used for getting and setting value. 应该使用.val()方法来获取和设置值。

This means that unless you're dealing with disabled properties, use: 这意味着除非您正在处理禁用的属性,否则请使用:

$('input').val();

Also as a side note, the link you posted , after running tests, shows that val() is faster than prop() . 另外作为旁注,运行测试后发布的链接显示val()prop()更快。

Since you LOVE to show some graphs and pictures, i'm going to add some: 既然你喜欢显示一些图形和图片,我将添加一些:

Taken from this link 取自此链接

纯JS更好

Pure JS is better if you're arguing over speed. 如果你在争论速度,纯粹的JS会更好。

Dude, you cannot compare prop to val , that makes no sense! 老兄,你无法将道具val进行比较,这没有任何意义!

val is designed to set/get the attribute "value" from the current selected node(s). val旨在设置/获取当前所选节点的属性“value”。 So that's the only method you need for your use-case. 这是您使用案例所需的唯一方法。 (I want to set an input text value). (我想设置输入文本值)。

Of course you can workaround val by using attr('value') or prop('value') but why do you want to make boilerplate code? 当然你可以通过使用attr('value')prop('value')来解决val ,但为什么要制作样板代码?

You seem to be confused on what prop was designed for. 你似乎对道具的设计感到困惑。 So let me explain a little. 所以让我解释一下。

prop was designed to solve the issue of retrieving Boolean-like properties from the selected DOM node. prop旨在解决从所选DOM节点检索类布尔属性的问题。 It should be compared to the method attr not to val 应将其与attr方法进行比较,而不是val

Consider the following four cases 考虑以下四种情况

<input type="checkbox" checked />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="true" />
  • You will never ever get the state checked/unchecked of the input using val() 使用val()永远不会检查/取消选中输入的状态
  • You are not sure what is the checked state of the control if you retrieve it using attr('checked') . 如果使用attr('checked')检索它,则不确定控件的已检查状态是什么。 This will give you back a string. 这会给你一个字符串。
  • If you use the result of attr('checked') inside of an if statement, you are at risk of getting false positives, eg, if('false') 如果在if语句中使用attr('checked')的结果,则可能会出现误报,例如if('false')

The only way to consistently get the checked state out of the control is by using the prop method. 始终将检查状态从控件中取出的唯一方法是使用prop方法。

So if you wanna make a real comparison please compare it to attr , not to val 因此,如果您想进行真正的比较,请将其与attr进行比较,而不是与val进行比较

Your charts are indicating that prop() is better than val() in terms of performance. 您的图表表明prop() val()在性能方面优于val()

val() could be internally using the same logic of prop() along with some additional code statements for completing the actual operation(Ex: setting value), that additional code of val() might lead to little low performance than prop() . val()可以在内部使用prop()的相同逻辑以及一些额外的代码语句来完成实际操作(例如:设置值), val()附加代码可能导致性能低于prop()

It would be nice to use val() for setting a value for an element perhaps that is what this function is meant for, in general it's preferred to go with the operation specific functions instead of using the generic ones. 使用val()来设置一个元素的值可能就是这个函数的意思,这通常会优先使用特定于操作的函数,而不是使用泛型函数。

In your case, if you don't want to loose even a slight variation in performance then go with prop() itself. 在你的情况下,如果你不想松散甚至性能的轻微变化,那么请使用prop()本身。

The other answers explain the main differences pretty well with one exception. 其他答案很好地解释了一个例外的主要差异。

Since jQuery "normalizes" browser differences val() removes carriage returns. 由于jQuery“规范化”浏览器差异,因此val()会删除回车符。

This affects textareas in IE 8 and below . 这会影响IE 8及更低版本中的 textareas。

Example: For the textarea input of 示例:对于textarea输入

HEL


LO

.val() gives you the string "HEL\\n\\nLO" and a length of 7 .val()为您提供字符串"HEL\\n\\nLO" ,长度为7

.prop("value") gives you the string "HEL\\r\\n\\r\\nLO" with a length of 9 .prop("value")为您提供长度为9的字符串"HEL\\r\\n\\r\\nLO"

That's not a question of performance, they are actually made for quite different things. 这不是性能问题,它们实际上是针对完全不同的东西而制作的。 jQuery seems to have decided to handled "out of spec" usecase, probably for good reason. jQuery似乎决定处理“超出规范”的用例,可能是有充分理由的。

  • attr() is to access the HTML attribute of the element. attr()是访问元素的HTML属性。 More probably the initial value as when the page loaded. 更可能是页面加载时的初始值。 It though has been altered by the write version of attr() or any other attribute manipulation function 它虽然已被attr()的写版本或任何其他属性操作函数所改变
  • prop() is to access the DOM property of the element, after it has been parsed and displayed. prop()是在解析并显示元素之后访问元素的DOM属性。
  • val() is to access a form element value. val()用于访问表单元素值。

One more bug when using prop() . 使用prop()时还有一个bug。 Under IE 11, the following code wouldn't set the value: 在IE 11下,以下代码不会设置值:

var props = {'value':'test', 'type':'radio'};
var input = $('<input/>').prop(props);
$(document.body).append(input);

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

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