开发者

changing value of `value` attribute for an `<input>` element

Why can't .attr() change the value of value?开发者_JS百科

$("input").attr("value") = "New text";

See also a live jsfiddle example


In general: Because the value attribute sets the default value, not the current value. Use the val() method instead.

$("input").val("New text");

In your specific case (because the value hasn't be changed by the user): because the attr method doesn't give you something you can assign a value to (and can't be written in such a way as would allow you to). It takes two arguments, the second of which is the value you want to assign.

$("input").attr("value", "New text");


Replace $("input").attr("value") = "New text";

with $("input").attr("value","New text");

attr( attributeName, value )

That is the proper signature for attr


You should do

$("input").val("New text");


This code may help you :

 $("input").attr(attributName , valueYouWantToGiveToTheAttribute)

The attr function first parameter is the attribute name and the second option the value that you want to give to the attribute. Via this function, you can add attributes to the element and also edit attributes value of an element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜