By default, the jQuery function will html()save the properties defaultValueand defaultChecked, rather than using the current values / selection (as JavaScript does innerHTML). To get around this, you need to use jQuery first prop(): Read the prop () documentation .
For example, given the code:
<form action='/'>
<input type='radio' value='1' checked name='first' />
<input type='radio' value='2' name='first' />
<input type='checkbox' value='yes' />
<input type='text' value='Start Text' name='second' />
</form>
then
$('form').html()
It always displays this exact code no matter what values you added / selected / changed.
, :
$("form input[type='radio']").each(function(){
if (this.checked){
$(this).prop("defaultChecked",true);
} else {
$(this).prop("defaultChecked",false);
}
});
$("form input[type='checkbox']").each(function(){
if (this.checked){
$(this).prop("defaultChecked",true);
} else {
$(this).prop("defaultChecked",false);
}
});
$("form input[type='text']").each(function(){
$(this).prop("defaultValue",$(this).val());
});
alert( $(form).html() );
HTML, .