1

最初,表单可能具有如下字段:

<input type="text" name="Age" value="" />

反正你填34

很简单,我想存储 AS LAST STATE 形式的缓存快照,如下所示:

<script language="JavaScript">
document.getElementById('cached').value=document.getElementById('form1').innerHTML;
</script>

但是,我得到的结果只是:

<input type="text" name="Age" value="" />

与我想要的 34 的值:

<input type="text" name="Age" value="34" />

有什么方法可以获得“实时”的innerHTML?我肯定会在这里接受一个 jQuery 解决方案。谢谢!

4

2 回答 2

0

@all,这对我有用:

//http://stackoverflow.com/questions/1388893/jquery-html-in-firefox-uses-innerhtml-ignores-dom-changes
(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);

这给我的不是表单数组,而是表单 WITH mods 在提交时对数据的“innerHTML”。这是在给定时间重新创建表单的一种快速简便的方法 - 如果您愿意,可以使用快照。我在多步骤过程中使用它,以允许用户快速“返回”按钮选项,该选项不涉及数据库或需要大量 PHP 处理。

于 2013-10-16T08:43:57.367 回答
0

甚至 jquery 也会返回相同的结果。你需要在这里做一些技巧。因为当你填写一个字段时,你并没有真正设置 html 的专有值。

var snapshot = $('#field_ID').clone();
var ssv = $('#field_ID').val();
var snapshot = snapshot.attr('value' , ssv);

现在如果你使用

$('body').append(snapshot);

您将获得具有 value 的字段。

何时拍摄快照

知道何时拍摄这些快照的最佳方法是用户开箱即用的焦点

$('#field_ID').focusout(function(){
    var snapshot = $(this).clone();
    var ssv = $(this).val();
    var snapshot = snapshot.attr('value' , ssv);
    $('body').append(snapshot);
})
于 2013-10-11T22:18:58.087 回答