简体   繁体   English

jQuery的clone()函数混淆了原始元素的属性

[英]jQuery's clone() function messes up original element's property

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. 将它们克隆到隐藏的div中,然后检查一个不检查另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM