修改input【text】的值用jquery中的html()方法在Firefox中得出来为空

为乐而来 2013-07-11 01:54:54

有一段代码如下:
<script language="javascript">
$(function () {
$("#btn1").click(function(){
$("#div2").append($("#div1").html());
});
});
</script>

<div id="div1">
<input name="text1" type="text" value="111" />
<input name="text2" type="text" value="" />
</div>
<div id="div2"></div>
<input id="btn1" type="button" value="Copy" />

在浏览器显示如下:


我IE里先修改text2的值,再点击Copy按钮后(把div1里面的html复制到div2中),这个时候是正常的,如下图,可以把text2修改后的值一起复制到div2中


下面用Firefox打开,先修改text2的值,再点击copy,这个时候只复制了原来生成的html,text2的中值是空的,如下图,

请问这个问题如何解决,哪位大哥遇到过
...全文
209 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
AI头条 2014-03-30
  • 打赏
  • 举报
回复
引用 4 楼 q149072205 的回复:
在网上找了别人解决的办法,另外写一个方法formhtml()来替代.html()方法 (function ($) { var oldHTML = $.fn.html; $.fn.formhtml = function () { if (arguments.length) return oldHTML.apply(this, arguments); $("input,textarea,button", this).each(function () { this.setAttribute('value', this.value); }); $(":radio,: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);
解决了我的问题,谢谢。
我是地摊小贩 2013-07-30
  • 打赏
  • 举报
回复
。。。。在FF中,他只读取你div1中原始的html。div1中你text2中本来是没有内容的。。。
为乐而来 2013-07-30
  • 打赏
  • 举报
回复
在网上找了别人解决的办法,另外写一个方法formhtml()来替代.html()方法 (function ($) { var oldHTML = $.fn.html; $.fn.formhtml = function () { if (arguments.length) return oldHTML.apply(this, arguments); $("input,textarea,button", this).each(function () { this.setAttribute('value', this.value); }); $(":radio,: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);
ztszhq 2013-07-30
  • 打赏
  • 举报
回复
引用 2 楼 q149072205 的回复:
[quote=引用 1 楼 u011096386 的回复:] $("#div2").append($("#div1").html()); 换为 $("#div2").append($("#div1").clone());
这个不行。。[/quote] 这是 测试过的 怎么不行?值还是空? 你代码贴上来
为乐而来 2013-07-30
  • 打赏
  • 举报
回复
引用 1 楼 u011096386 的回复:
$("#div2").append($("#div1").html()); 换为 $("#div2").append($("#div1").clone());
这个不行。。
ztszhq 2013-07-11
  • 打赏
  • 举报
回复
$("#div2").append($("#div1").html()); 换为 $("#div2").append($("#div1").clone());

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧