首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2013-08-09 
对input【text】的值在jquery中的html()方法在Firefox中得出来为空有一段代码如下: script languagejavas

对input【text】的值在jquery中的html()方法在Firefox中得出来为空
有一段代码如下: 
<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" /> 


在浏览器显示如下: 
对input【text】的值在jquery中的html()步骤在Firefox中得出来为空

我在IE里先修改text2的值,再点击Copy按钮后(把div1里面的html复制到div2中),这个时候是正常的,如下图,
对input【text】的值在jquery中的html()步骤在Firefox中得出来为空
可以把text2修改后的值一起复制到div2中 下面用Firefox打开,先修改text2的值,再点击copy,这个时候只复制了原来生成的html,text2的中值是空的,如下图,
对input【text】的值在jquery中的html()步骤在Firefox中得出来为空
请问这个问题如何解决,哪位大哥遇到过 
[解决办法]
看到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>

[解决办法]
试试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>


[解决办法]
$("#div2").append($("#div1").clone()); 
[解决办法]
引用:
Quote: 引用:

试试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);


解决了就好

热点排行