得到innerHTML已经改变了

时间:2013-10-11 22:12:51

标签: javascript jquery

最初,表单可能包含以下字段:

<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="" />

VS。我想要的是34的值:

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

任何获得“实时”innerHTML的方式?我肯定会在这里接受一个jQuery解决方案。谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

现在使用

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

您将获得具有该值的字段。

何时拍摄快照

了解何时拍摄这些快照的最佳方式是用户开箱即用时

$('#field_ID').focusout(function(){
    var snapshot = $(this).clone();
    var ssv = $(this).val();
    var snapshot = snapshot.attr('value' , ssv);
    $('body').append(snapshot);
})

答案 1 :(得分: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处理。

相关问题