JQuery-元素未被复制?

JQuery-元素未被复制?,jquery,Jquery,复制页面DOM中的元素时遇到问题。以下是我要复制的元素的代码: <fieldset class="step" id="person2_dummy"> <legend>Persoonskaart 2</legend> <ul class="form_column"> <li><label for="nickname_2">Roepnaam</label><input type=

复制页面DOM中的元素时遇到问题。以下是我要复制的元素的代码:

<fieldset class="step" id="person2_dummy">
    <legend>Persoonskaart 2</legend>
    <ul class="form_column">
        <li><label for="nickname_2">Roepnaam</label><input type="text" class="inputtext required" id="nickname_2_dummy" name="person2[nickname]"></li>
        <li><label for="prepositions_2">Tussenvoegsel(s)</label><input type="text" class="inputtext" id="prepositions_2_dummy" name="person2[prepositions]"></li>
        <li><label for="surname_2">Achternaam</label><input type="text" class="inputtext required" id="surname_2_dummy" name="person2[surname]"></li>
        <li>&nbsp;</li>
        <li><label for="bdate_day_2">Geboortedatum</label>
            <select class="required" name="person2[bdate_day]" id="bdate_day_2_dummy">
                <option value="">--</option>
                <option value="1">1</option>
            </select>
            <select class="required" name="person2[bdate_month]" id="bdate_month_2_dummy">
                <option value="">--</option>
                <option value="1">januari</option>
            </select>
            <select class="required" name="person2[bdate_year]" id="bdate_year_2_dummy">
                <option value="">--</option>
                <option value="2011">2011</option>
            </select>
        </li>
        <li><label for="sex_2">Geslacht</label><select class="inputselect required" id="sex_2_dummy" name="person2[sex]">
            <option value="MALE">Man</option>
            <option value="FEMALE">Vrouw</option>
        </select></li>
        <li>&nbsp;</li>
        <li><label for="national_id_2">BSN / Sofinummer</label><input type="text" class="inputtext required" id="national_id_2_dummy" name="person2[national_id]"><a class="tooltip" href="#" oldtitle="Uitleg BSN/Sofienummer"><img alt="" src="/cmsfile/70"></a></li>
            <li><label for="second_driver">Tweede bestuurder</label><input type="checkbox" class="inputtext" id="second_driver_dummy" name="second_driver"></li>
            <li style="visibility: hidden;" id="li_second_driver"><label for="license_2_dummy">Rijbewijsnummer</label><input type="text" class="inputtext" id="license_2_dummy" name="person2[license]"><script type="text/javascript">/*&lt;![CDATA[*/ 
            $("#second_driver").click(function() {
                if ($("#second_driver").is(":checked")) {
                    $("#li_second_driver").css("visibility", "visible");
                    $("#license_2").addClass("required");
                } else {
                    $("#li_second_driver").css("visibility", "hidden");
                    $("#license_2").removeClass("required");
                }
                determineOrderType();
            }); /*]]&gt;*/</script><br clear="all"></li>
        <li><label for="ice_1_2">Alarmnummer 1</label><input type="text" class="inputtext required" id="ice_1_2_dummy" name="person2[ice_1]"><a class="tooltip" href="#" oldtitle="Uitleg alarmnummer"><img alt="" src="/cmsfile/70"></a></li>
        <li><label for="ice_2_2">Alarmnummer 2</label><input type="text" class="inputtext" id="ice_2_2_dummy" name="person2[ice_2]"></li>
    </ul>
    <div style="display: none;">
        <label for="no-allergies_2_dummy">Geen allergieen</label><input type="checkbox" checked="checked" id="no-allergies_2_dummy" value="no-allergies" name="person2[allergies][]">
        <label for="no-blood-thinners_2_dummy">Geen bloedverdunners</label><input type="checkbox" checked="checked" id="no-blood-thinners_2_dummy" value="no-blood-thinners" name="person2[bloodthinners][]">
        <label for="no-heart-diseases_2_dummy">Geen hartaandoeningen</label><input type="checkbox" checked="checked" id="no-heart-diseases_2_dummy" value="no-heart-diseases" name="person2[heartdiseases][]">
    </div>
    <div class="form_column" id="medical_info_2_dummy">
        <ul class="form_medical">
            <li><em>Medische allergieen</em></li>
            <li><label for="lodine_2">Jodium</label>        <input type="checkbox" id="lodine_2_dummy" value="lodine" name="person2[allergies][]" class="inputcheckbox"></li>
            <li><label for="antibiotics_2">Antibiotica</label>  <input type="checkbox" id="antibiotics_2_dummy" value="antibiotics" name="person2[allergies][]"></li>
            <li><label for="contrast_2">Contrast</label>        <input type="checkbox" id="contrast_2_dummy" value="contrast" name="person2[allergies][]"></li>
            <li><label for="latex_2">Latex</label>      <input type="checkbox" id="latex_2_dummy" value="latex" name="person2[allergies][]"></li>
            <li><label for="penicillin_2">Penicilline</label>   <input type="checkbox" id="penicillin_2_dummy" value="penicillin" name="person2[allergies][]"></li> 

            <li><em>Hartaandoeningen</em></li>
            <li><label for="pacemaker_2">Pacemaker / ICD</label>    <input type="checkbox" id="pacemaker_2_dummy" value="pacemaker" name="person2[heartdiseases][]"></li>
            <li><label for="beta-blocker_2">Betablocker</label> <input type="checkbox" id="beta-blocker_2_dummy" value="beta-blocker" name="person2[heartdiseases][]"></li>
        </ul>
        <ul class="form_medical">
            <li><em>Gebruik van bloedverdunners</em></li>
            <li><label for="carbasalaatcalcium_2">Ascal</label> <input type="checkbox" id="carbasalaatcalcium_2_dummy" value="carbasalaatcalcium" name="person2[bloodthinners][]"></li>
            <li><label for="clopidogrel_2">Plavix</label>       <input type="checkbox" id="clopidogrel_2_dummy" value="clopidogrel" name="person2[bloodthinners][]"></li>
            <li><label for="acenocoumarol_2">Acenocoumarol</label><input type="checkbox" id="acenocoumarol_2_dummy" value="acenocoumarol" name="person2[bloodthinners][]"></li>
            <li><label for="fenprocoumon_2">Marcoumar</label>   <input type="checkbox" id="fenprocoumon_2_dummy" value="fenprocoumon" name="person2[bloodthinners][]"></li>

            <li><em>Overige</em></li>
            <li><label for="diabetes_2">Diabetes</label>        <input type="checkbox" id="diabetes_2_dummy" value="diabetes" name="person2[other][]"></li>
            <li><label for="deaf_2">Doof</label>            <input type="checkbox" id="deaf_2_dummy" value="deaf" name="person2[other][]"></li>
        </ul>
    </div><script type="text/javascript">/*&lt;![CDATA[*/ 
            $(document).ready(function() {
            $("#medical_info_2_dummy").find("input").each(function() {
                $(this).click(function() {
                    handleMedical(2, $(this));
                });
            });
        }); /*]]&gt;*/</script></fieldset>

佩索恩斯卡特2号
  • 罗普南
  • 塔森沃塞尔(s)
  • 阿切特南
  • Geboortedatam -- 1. -- 一月 -- 2011
  • 格斯拉赫特 成年男子 沃鲁
  • BSN/Sofinummer
  • 特威德贝斯图德酒店
  • Rijbewijsnummer/*![CDATA[*/ $(“#第二个驱动程序”)。单击(函数(){ 如果($(“#第二个#驱动程序”)。为(“:选中”)){ $(“#li#u second_driver”).css(“可见性”、“可见”); $(“许可证2”).addClass(“必需”); }否则{ $(“#li#u second_driver”).css(“可见性”、“隐藏”); $(“许可证2”)。removeClass(“必需”); } determineOrderType(); });/*]*/
  • Alarmnummer 1
  • Alarmnummer 2
吉恩·艾尔吉恩 吉恩·布卢德威尔德斯 吉恩·哈塔恩多尼根
  • 麦地奇过敏原
  • 乔迪姆
  • 抗生素
  • 对比度
  • 乳胶
  • 青霉素
  • 哈塔恩多宁
  • 心脏起搏器/ICD
  • 贝塔布洛克
  • 格布吕克·范布卢德维尔登斯酒店
  • 阿斯卡尔
  • 波立维
  • 乙酰香豆醇
  • 马尔库马尔
  • 过度疲劳
  • 糖尿病
  • 末日
/*![CDATA[*/ $(文档).ready(函数(){ $(“#医疗信息_2_虚拟”)。查找(“输入”)。每个(函数(){ $(此)。单击(函数(){ handleMedical(2,$(本)); }); }); }); /*]]*/
我试图复制person2_伪元素,删除所有的伪id,并将其插入页面上的另一个元素之后。像这样:

    var number = 2;
var after = $('#person1'); // <-- yes, this does exist

$("#person"+number+"_dummy").clone().attr("id", "person" + number).find("input, select, div").each(function(){
    alert($(this).attr("id"));
    $(this).attr("id", function(){
        var attr = $(this).attr("id");
        return attr.replace("_dummy", "");
    })
}).end().insertAfter(after);
var编号=2;
var after=$(“#person1”);//
编辑:
如果这不起作用,您的#person1 div/元素不存在或找不到。
如果将“插入后”更改为:

insertAfter("#person"+number+"_dummy")
它将直接在复制的div之后插入div(参见此jsFiddle)

啊,废话

我要责备我的同事添加了一个狡猾的工具提示插件。代码在jFiddle上工作,因此它必须位于页面的其余部分。虽然它没有抛出错误,但该组件将javascript搞乱了。Grr

所以,我删除了插件(qTip顺便说一句),一切都像一个魅力!
非常感谢大家的想法。

只是好奇,为什么要设置
id
两次?如果您希望它是泛型的,
.attr()
处理程序将获得参数,如下所示:
.attr(“id”,function(i,id){return id.replace(“\u dummy”,”);})
…不需要
。each()
循环等:)某些事情发生了。放入一些调试警报。使用Firebug来发现错误。使用断点跟踪脚本执行。@这段代码还用于更改其他属性。你的意思是:$(“#person”+number+“#dummy”).clone().attr(“id”,函数(i,id){return id.replace(“#dummy”,”);}).end().insertAfter(after)@Tomalak:我试过了,事实上,在这段代码中还有一个测试警报:)我试过在这段代码之前和之后发出警报,一切都正常。Firebug中也没有错误,在发布到这里之前我做了一些家庭作业。@Robbert-几乎,在需要的地方使用该函数,例如
.find(“whatever”).andSelf().attr(…就像我上面所说的…).end().end().insertAfter()
,或者更干净一点,插入然后更改ID,无论是哪一个。是的,我看到了编辑,谢谢,但还是没有雪茄。问题是,即使这样也不行:$(“#person”+number+“#dummy”).clone().insertAfter(after);那么一定还有别的事情发生了?顺便说一句,person1和person2_虚拟元素都被找到了!(通过提醒他们的.attr(“id”)进行检查)您可以在:(抱歉,都是荷兰语:)上找到完整的源代码。顺便说一下:表单顶部有一个链接,它将尝试将字段集添加到DOM中。
insertAfter("#person"+number+"_dummy")