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

为乐而来 2013-07-30 11:07:35
有一段代码如下:
<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的中值是空的,如下图,

请问这个问题如何解决,哪位大哥遇到过
...全文
171 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiubin200236 2013-07-30
  • 打赏
  • 举报
回复
$("#div2").append($("#div1").clone());
街头小贩 2013-07-30
  • 打赏
  • 举报
回复
试试clone

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head>

<body>
<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" /> 
<script type="text/javascript">
jQuery(function($){

	$("#btn1").click(function(){ 
		$("#div1 input").clone().prependTo('#div2');
	});
});

</script>
</body>
</html>

潮起潮落 2013-07-30
  • 打赏
  • 举报
回复
看到stackoverflow上面也没有好的解决办法 大多是这样间接地搞,clone后再手动赋值

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript"> 
$(function () { 
	$("#btn1").click(function(){ 
		$("#div2").append($("#div1").clone().find("input[name='text2']").attr("value",$("#div1 input[name='text2']").val()).parent()); 
	}); 
}); 
</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" /> 
</body>
</html>
街头小贩 2013-07-30
  • 打赏
  • 举报
回复
引用 4 楼 q149072205 的回复:
[quote=引用 2 楼 xiaofanku 的回复:] 试试clone

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head>

<body>
<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" /> 
<script type="text/javascript">
jQuery(function($){

	$("#btn1").click(function(){ 
		$("#div1 input").clone().prependTo('#div2');
	});
});

</script>
</body>
</html>

clone也不行,最后我在网上找到了重写一个formhtml方法能可以解决: (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);[/quote] 解决了就好
为乐而来 2013-07-30
  • 打赏
  • 举报
回复
引用 2 楼 xiaofanku 的回复:
试试clone

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head>

<body>
<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" /> 
<script type="text/javascript">
jQuery(function($){

	$("#btn1").click(function(){ 
		$("#div1 input").clone().prependTo('#div2');
	});
});

</script>
</body>
</html>

clone也不行,最后我在网上找到了重写一个formhtml方法能可以解决: (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);

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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