jQuery copy html and form values
i have a table with over 100 forms in it. i'm POSTing t开发者_运维问答he html of its container into a form (ignore how crude this may or may not be for now), but the form values are not part of the returned text. how can i adjust the below code to include the inputted form values?
var dd = escape($('#lb-content').html());
The problem might be in the innerHTML property which is basically what jQuery's html() method returns. It has not been standardized, and you might be getting different results for different browsers.
If you really need to go with such a crude solution, you might try to clone each input/select element, append it to the form, and delete the original just before posting. This might force html() to catch the "updated" values.
I have not tested this, so it might not work at all.
When browser parses <input value="...">
, this value becomes a "default" value. It is needed and kept in DOM model for "reset" functionality.
You need to store user's value somewhere else in the DOM model. Since collecting data post-factum is slow, it is better to store the values as user goes with the input, be it $('#lb-content input').change()
or any other handler. Then, you have two options: store the values in hidden valid elements (span
or similar), or use data-myownattr="val"
since it is valid in HTML5 to use custom attributes prefixed with data-
(see How to store arbitrary data for some HTML tags)
I just realized I had answered this before in another question: how to get generated html form with jquery
$('#lb-content input').each(function(){
$(this).attr('value', $(this).val()
});
console.log($('#lb-content').html());
精彩评论