我该如何克隆单选按钮?

时间:2014-03-11 05:59:52

标签: javascript jquery html

我在使用单选按钮部分克隆字段时出现问题,这是我的代码

<div id="optionBook1" class="clonedBook">
            <label for="txtSkillName1">
                <abbr title="Required">
                    <em><font color="red">*</font></em>
                </abbr>
                Skill Name
            </label>
                <input type="text" id="txtSkillName1" name="txtSkillName1" class="validate[required] inputLong"
                runat="server" />

                <p class="power"> 
                <label for="txtSkillLevel">
                    <abbr title="Required">
                        <em><font color="red">*</font></em>
                    </abbr>
                    Skill Level</label>
                <span>
                     <input type="hidden" id="txtSkillLevel1" name="txtSkillLevel1" class="validate[required] inputLong"
                    value="" />
                     <input type="radio" id="radiolvl1" name="radiolvl1" 
                    value="1" />
                     <input type="radio" id="radiolvl2" name="radiolvl2" 
                    value="2" />
                     <input type="radio" id="radiolvl3" name="radiolvl3" 
                    value="3" />
                     <input type="radio" id="radiolvl4" name="radiolvl4" 
                    value="4" />
                     <input type="radio" id="radiolvl5" name="radiolvl5" 
                    value="5" />
                </span>
            </p>
            <p>
                <label for="txtSkillDescription">
                    <abbr title="Required">
                        <em><font color="red">*</font></em>
                    </abbr>
                    Skill Description</label>
                <span>
                    <textarea style="overflow: auto; resize: none" rows="3" cols="50" id="txtSkillDescription1"
                        name="txtSkillDescription1" class="validate[required] inputLong"></textarea>
                </span>
            </p>

            </div>
            <input type="button" id="btnAdd1" value="Add" />
                 <input type="button" id="btnDel1" value="Remove" />

$('#btnAdd1').click(function () {

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#optionBook1').clone().attr('id', 'optionBook2');


        // manipulate the name/id values of the input inside the new element
    $(newElem).find('[id=txtSkillName1]').attr('name', 'txtSkillName2');    
    $(newElem).find('[id=txtSkillName1]').attr('id', 'txtSkillName2');

    $(newElem).find('[id=txtSkillLevel1]').attr('name', 'txtSkillLevel2');    
    $(newElem).find('[id=txtSkillLevel1]').attr('id', 'txtSkillLevel2');

    $(newElem).find('[id=txtSkillDescription1]').attr('name', 'txtSkillDescription2');    
    $(newElem).find('[id=txtSkillDescription1]').attr('id', 'txtSkillDescription2');

        // insert the new element after the last "duplicatable" input field
        $(newElem).insertAfter('#optionBook1');


    });

这里我需要隐藏技能领域..我想要实现的是每当我点击一个单选按钮时,该值将传递给skillvl字段,它将与克隆的skilllvl字段相同

感谢

1 个答案:

答案 0 :(得分:0)

试试这个,

JsFiddle Demo

$('#btnAdd1').click(function () {

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#optionBook1').clone().find('input:radio').end();
    $(newElem).find('input:radio').each(function(index, attr) { 
      $(this).attr('id',$(this).attr('id')+'_clone');
       $(this).attr('name',$(this).attr('id')+'_clone');

   });


        // manipulate the name/id values of the input inside the new element
    $(newElem).find('[id=txtSkillName1]').attr('name', 'txtSkillName2');    
    $(newElem).find('[id=txtSkillName1]').attr('id', 'txtSkillName2');

    $(newElem).find('[id=txtSkillLevel1]').attr('name', 'txtSkillLevel2');    
    $(newElem).find('[id=txtSkillLevel1]').attr('id', 'txtSkillLevel2');

    $(newElem).find('[id=txtSkillDescription1]').attr('name', 'txtSkillDescription2');    
    $(newElem).find('[id=txtSkillDescription1]').attr('id', 'txtSkillDescription2');

        // insert the new element after the last "duplicatable" input field
        $(newElem).insertAfter('#optionBook1');
$(newElem).attr('id','optionBook2')

    });