Jquery 增量ID能否解决克隆()的单选按钮行为问题?

Jquery 增量ID能否解决克隆()的单选按钮行为问题?,jquery,radio-button,nested,clone,increment,Jquery,Radio Button,Nested,Clone,Increment,我有几个问题,没有一个是我单独解决的。我通读了所有关于克隆单选按钮的问答,然后在这里发布之前尝试了许多(如果不是大多数的话)增量ID解决方案 成功的代码让我迷茫。底部有一个JSFIDLE链接 1) 我想使用增量更改克隆元素的名称和id 我[认为]更愿意使用这种方法,这种方法失败的主要原因是我不知道把什么放在哪里(即“?”)——另一种方法当然是受欢迎的: var regex = /^(.*)(\d)+$/i; var cloneIndex = $(".newloc").length; $("bu

我有几个问题,没有一个是我单独解决的。我通读了所有关于克隆单选按钮的问答,然后在这里发布之前尝试了许多(如果不是大多数的话)增量ID解决方案

成功的代码让我迷茫。底部有一个JSFIDLE链接

1) 我想使用增量更改克隆元素的名称和id

我[认为]更愿意使用这种方法,这种方法失败的主要原因是我不知道把什么放在哪里(即“?”)——另一种方法当然是受欢迎的:

var regex = /^(.*)(\d)+$/i;
var cloneIndex = $(".newloc").length;

$("button.clone").live("click", function(){
$(this).parents(".newloc").clone()
    .appendTo("#location_container")
    .attr("id", "newloc" +  cloneIndex)
    .find("??").each(function() {
        var id = this.id || "";
        var match = id.match(regex) || [];
        if (match.length == 3) {
            this.id = match[1] + (cloneIndex);
        }
});
cloneIndex++;
}))

2) 我的研究表明,前一种方法将解决克隆单选按钮的不当行为

3) 嵌套在克隆div中的是JQuery,它依赖于单选按钮选择,在源代码中运行,但在克隆中失败

4) 尽管我可能会尝试,但我无法为remove button函数找到一个remove last clone脚本——太简单了,我确信我错过了一些非常明显的东西。这失败了:

$("button.remove").live("click", function(){
$(this).parents(".newloc").remove();
}))

5) 如果屏幕向下滚动,在画面顶部显示新克隆的顶部,我会非常高兴

从理论上讲,这一切听起来相当容易,但在5天和一个周末之后,我感到困顿和沮丧,所以我呼吁你们提供帮助和帮助


非常感谢您的帮助

是的。我所有的问题都得到了回答

在一位才华横溢的加州大学伯克利分校学生的帮助下——谢谢杰瑞

$(document).ready(function () { 

    $('.remove').hide();//hiding the remove button
     var radioCounter = 0;
    // var prot = $(document).find('.newloc');

    $('.add').click(function () {

        //Preserve original location settings (first two in the list all times)
        //The reason for this if because when the form is first cloned, the names
        //of the radio buttons have not been changed yet, so the value of the original
        //radio buttons are lost, so we need to preserve them and then re-set them to
        //accordingly later.
        var originalConfVal = $('input[name="conference[0]"]:checked').val();
        var originalRoleVal = $('input[name="role[0]"]:checked').val();

        // this duplicates the section, and clears the values in the clone, adds a new class and places the clone
        $('.add_location').clone(true).find('input:not(:radio)').val("").end().removeClass("add_location").addClass("newloc").appendTo($('#location_container'));
        $('.newloc').find('.remove').show();
        //Set correct incremented names
        radioCounter += 1;

        var $allConfs = $('.authradio[name^="conference["]');
        var $allRoles = $('.authradio[name^="role["]');

        //Get last two conference authradios and set incremented names
        $allConfs.slice(-2, $allConfs.length).attr('name', 'conference[' + radioCounter + ']');

        //Get last 3 role authradios and set incremented names
        $allRoles.slice(-3, $allRoles.length).attr('name', 'role[' + radioCounter + ']');

        //Make the newly cloned radio buttons be checked at "no" for conference
        $('input[name="conference[' + radioCounter + ']"][value="no"]').prop("checked", "checked");
        $('input[name="role[' + radioCounter + ']"][value="Attendee"]').prop("checked", "checked");

        //Hide new sliding conference menu
        $('.hideme:last').hide();

        //Reset original radio buttons to preserved values
        $('input[name="conference[0]"][value="' + originalConfVal + '"]').prop("checked", "checked");
        $('input[name="role[0]"][value="' + originalRoleVal + '"]').prop("checked", "checked");

// -- ---------------------------------------------------- -->
        // this fixes the date picker repetition issue
        $('.authdepart:last').removeClass('hasDatepicker').attr('id', "").datepicker({
            numberOfMonths: 3,
            showButtonPanel: true
        });
        $('.authreturn:last').removeClass('hasDatepicker').attr('id', "").datepicker({
            numberOfMonths: 3,
            showButtonPanel: true
        });

        // clone styling
         $('.newloc:last').prepend(
        $('<div/>').addClass("auth_98").css('border', 'none').append(
        $('<legend/>').append('New Location')));    

         //Scroll container
        $('#location_container').animate({scrollTop: $('#location_container')[0].scrollHeight}, "slow");

    });

    $('.hideme').hide();

    $('.authradio').on('change', function () {
        if ($(this).val() == 'yes') $(this).parent().find('.hideme').slideDown();
        else $(this).parent().find('.hideme').slideUp();
    });

    $('.datepicker').datepicker({
        numberOfMonths: 3,
        showButtonPanel: true
    });

    $("button.remove").on("click", function(){
        $(this).parents(".newloc").remove();
   });

});
$(文档).ready(函数(){
$('.remove').hide();//隐藏删除按钮
var放射性计数器=0;
//var prot=$(document.find('.newloc');
$('.add')。单击(函数(){
//保留原始位置设置(始终保留列表中的前两个)
//之所以会出现这种情况,是因为在首次克隆表单时
//单选按钮的值尚未更改,因此原始值
//单选按钮丢失,因此我们需要保留它们,然后将它们重新设置为
//相应地,以后。
var originalConfVal=$('input[name=“conference[0]”):选中').val();
var originalRoleVal=$('input[name=“role[0]”]:checked').val();
//这将复制节,并清除克隆中的值,添加新类并放置克隆
$('.add_location').clone(true).find('input:not(:radio)).val(“”.end().removeClass(“add_location”).addClass(“newloc”).appendTo($('#location_container');
$('.newloc').find('.remove').show();
//设置正确的递增名称
无线电计数器+=1;
var$allConfs=$('.authradio[name^=“conference[“]”);
var$allRoles=$('.authradio[name^=“role[“]”);
//获取最后两个会议授权收音机并设置递增的名称
$allConfs.slice(-2,$allConfs.length).attr('name','conference['+radioCounter+']);
//获取最后3个角色身份验证收音机并设置递增的名称
$allRoles.slice(-3,$allRoles.length).attr('name','role['+radioCounter+']);
//使新克隆的单选按钮在会议的“否”处被选中
$('input[name=“conference['+radioCounter+']”][value=“no”].prop(“checked”、“checked”);
$('input[name=“role['+radioCounter+']”][value=“Attendee”].prop(“checked”、“checked”);
//隐藏新的滑动会议菜单
$('.hideme:last').hide();
//将原始单选按钮重置为保留值
$('input[name=“conference[0]”][value=“”+originalConfVal+“]”)prop(“选中”、“选中”);
$('input[name=“role[0]”][value=“”+originalRoleVal+“]”].prop(“选中”、“选中”);
// -- ---------------------------------------------------- -->
//这修复了日期选择器重复问题
$('.authdepeat:last').removeClass('hasDatepicker').attr('id',').datepicker({
月数:3,
showButtonPanel:对
});
$('.authreturn:last').removeClass('hasDatepicker').attr('id',').datepicker({
月数:3,
showButtonPanel:对
});
//克隆样式
$('.newloc:last')。前缀(
$('').addClass(“auth_98”).css(“边框”,“无”).append(
$('').append('New Location');
//滚动容器
$(“#位置#容器”)。动画({scrollTop:$(“#位置#容器”)[0]。scrollHeight},“slow”);
});
$('.hideme').hide();
$('.authradio')。在('change',函数(){
if($(this.val()=='yes')$(this.parent().find('.hideme').slideDown();
else$(this.parent().find('.hideme').slideUp();
});
$('.datepicker').datepicker({
月数:3,
showButtonPanel:对
});
$(“button.remove”)。在(“单击”,函数(){
$(this.parents(“.newloc”).remove();
});
});