3 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
Firefox沒有更新的value 屬性基于用戶輸入DOM對象的,只是它的value 性能 -相當(dāng)快,工作的周邊存在。
DOM:
function DisplayTextBoxValue(){
var element = document.getElementById('textbox');
// set the attribute on the DOM Element by hand - will update the innerHTML
element.setAttribute('value', element.value);
alert(document.getElementById("container").innerHTML);
return false;
}
.formhtml()自動執(zhí)行此操作的jQuery插件:
(function($) {
var oldHTML = $.fn.html;
$.fn.formhtml = function() {
if (arguments.length) return oldHTML.apply(this,arguments);
$("input,button", this).each(function() {
this.setAttribute('value',this.value);
});
$("textarea", this).each(function() {
// updated - thanks Raja & Dr. Fred!
$(this).text(this.value);
});
$("input:radio,input:checkbox", this).each(function() {
// im not really even sure you need to do this for "checked"
// but what the heck, better safe than sorry
if (this.checked) this.setAttribute('checked', 'checked');
else this.removeAttribute('checked');
});
$("option", this).each(function() {
// also not sure, but, better safe...
if (this.selected) this.setAttribute('selected', 'selected');
else this.removeAttribute('selected');
});
return oldHTML.apply(this);
};
//optional to override real .html() if you want
// $.fn.html = $.fn.formhtml;
})(jQuery);

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
感謝您的formhtml插件。為了配合使用textarea,我必須對其進(jìn)行更新:
$("textarea", this).each(function() {
$(this).text($(this).val());
});
添加回答
舉報(bào)