简体   繁体   中英

jQuery - why attr() does not consistently update displayed value on screen

here's two lines of jQuery

$("input[name=displayPrice1]").attr("value", vPrice);
$("input[name=displayPrice1]").val(vPrice);

both lines update the value of an input of type text.

here is a fiddle http://jsfiddle.net/fZm7w/

follow these steps:
1. after page load: click set attr() - the empty input will take the value of 150
2. click on append row and click again on set attr() - both input will get at 150
3. change manually the values in both inputs - say with 500 and 100
4. click set attr() - none of the values displayed will change , but the value attribute will change
5. click on set val() - both displayed values will be updated but not the attr value, as expected

Why does set attr() sets the value on screen when the input is empty but not when it has been filled with content by the user?

.val() gives the actual value that is in input field

.attr() is only getting attribute value in source of input field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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