您可以尝试在整个文档上设置一个实时事件,该事件将设置属性为HTML,并且用户设置值或在提交时设置它们。
例如
$("body").on("change", "select,input,textarea", function(){
$(this).attr("value", $(this).val());
});
第一种方式,但本不应该如此盲目做,你会得到与复位问题。你应该解决与选定的电台,复选框和其他属性的问题,不仅值。
第二种方法是在真正需要时序列化整个页面。
var serialize = function(el){
$("select, input, textarea").each(function(){
$(this).attr("value", $(this).val()); //the same way as upper
});
}
$(".serialize").click(function(){
var inner = $("body"),
html;
serialize(inner);
html = inner.html(); //here you will get whole html with setted attributes
});
这种方式似乎更好,因为那里不会是不必要的事件的委派。
但是在两种方式中,将永久值设置为DOM本身并不是一个好主意。