I am using jQuery to read data in a HTML page. On that page, I have a <form>
element which contains some <input>
tags for user to enter their values.
When being initialized, this form has some default values for those <input>
elements. When I submit this form, I use the html()
method to get the HTML string of current state of this page, but I do not why this result string is still contain old values (default input values), not the new ones.
Is there any way to update or persist the user-entered value to those <input>
elements before calling html()
? How can I get the string which contain newest value by using jQuery methods?
Thank you so much!
Example of jQuery get form values to get the form data with the serializeArray () function – When we entered the data and click on the button, the output is – In the above code, there is a form that contains some input elements and submits button.
.html( htmlString )Returns: jQuery. Description: Set the HTML contents of each element in the set of matched elements. A string of HTML to set as the content of each matched element. A function returning the HTML content to set.
When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.
When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content. Consider the following HTML:
However, it is strange what you are trying to do, but all you need is to update the value attribute of the form elements. It can be done with the following code (put it in the form submit
handler):
$("form input").attr("value", function() {
return this.value;
});
DEMO: http://jsfiddle.net/BXha6/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With