jQuery的clone()函数会混淆原始元素的属性 [英] jQuery's clone() function messes up original element's property

查看:109
本文介绍了jQuery的clone()函数会混淆原始元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个看似简单的问题。我先检查一个单选按钮,然后克隆它。原始单选按钮变为未选中,克隆的单选按钮是正确的。任何人都可以告诉我为什么原始的单选按钮不被选中?



提前感谢!

 code><!DOCTYPE HTML> 
< html>
< head>
< script src =jquery.js>< / script>
< / head>
< body>
< div id =ClonedDivstyle =display:none>< / div>
< div id =RadioDiv>
< input id =hightype =radioname =severityvalue =高/>
< input id =mediumtype =radioname =severityvalue =Medium/>
< input id =lowtype =radioname =severityvalue =Low/>
< / div>
< script>
$(#RadioDiv #high)。prop(checked,true);
alert(RadioDiv's High is:+($(#RadioDiv #high)[0] .checked?CHECKED。:NOT CHECKED!));
$(#ClonedDiv)。empty();
$(#RadioDiv> input)。clone()。appendTo(#ClonedDiv); //我只是将RadioDiv的输入克隆到ClonedDiv ...
alert(RadioDiv's High is:+($(#RadioDiv #high)[0] .checked?CHECKED。:NOT CHECKED!));
alert(ClonedDiv's High is:+($(#ClonedDiv #high)[0] .checked?CHECKED。:NOT CHECKED!));
< / script>
< / body>
< / html>


解决方案

可以检查组中只有一个单选按钮。检查一个取消检查所有其他人。一组单选按钮由他们都具有相同的名称属性确定。将它们克隆到隐藏的div中,然后检查一个un-checking对方。


I have this seemingly simple problem. I first check a radio button, then clone it. The original radio button becomes unchecked, and the cloned one is correct. Can anyone tell me why the original radio button becomes unchecked?

Thanks in advance!

<!DOCTYPE HTML>
<html>
<head>
    <script src="jquery.js"></script>
</head>
<body>
    <div id="ClonedDiv" style="display:none"></div>
    <div id="RadioDiv">
        <input id="high"   type="radio" name="severity" value="High"/>
        <input id="medium" type="radio" name="severity" value="Medium"/>
        <input id="low"    type="radio" name="severity" value="Low"/>
    </div>
    <script>
            $("#RadioDiv #high").prop("checked", true);
            alert("RadioDiv's High is: " + ($("#RadioDiv #high")[0].checked ? "CHECKED." : "NOT CHECKED!") );
            $("#ClonedDiv").empty();
            $("#RadioDiv>input").clone().appendTo("#ClonedDiv"); // I'm merely cloning RadioDiv's inputs into ClonedDiv...
            alert("RadioDiv's High is: " + ($("#RadioDiv #high")[0].checked ? "CHECKED." : "NOT CHECKED!") );
            alert("ClonedDiv's High is: " + ($("#ClonedDiv #high")[0].checked ? "CHECKED." : "NOT CHECKED!") );
    </script>
</body>
</html>

解决方案

Only one radio button in a group can be checked. Checking one un-checks all others. A grouping of radio buttons is determined by them all having the same name attribute. Cloning them into the hidden div and then checking one un-checks the other.

这篇关于jQuery的clone()函数会混淆原始元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆