在jQuery html操作之后,Radio Input被选中

Radio Input gets unchecked after jQuery html operation

本文关键字:Input Radio jQuery html 操作 之后      更新时间:2023-09-26

我得到一个非常奇怪的错误。在执行以下操作后,我的单选按钮被选中:

    var $page = $('[data-shortcode-page="' + shortcode + '"]', $webrock).html();
    //CHECKED
    console.log($('[data-shortcode-page="' + shortcode + '"] :checked', $webrock).length) 
    $('.webrock-page-content', $addPage).replaceWith($page);
    //UNCHECKED
    console.log($('[data-shortcode-page="' + shortcode + '"] :checked', $webrock).length)

有人知道为什么会这样吗?这里有一个jsFiddle: http://jsfiddle.net/mVB2q/1/

非常感谢!

您正在克隆具有相同名称的无线电组。需要更新克隆的无线组名称。下面是一个简单的解决方案,我将"test1"硬编码为新的组名,但您可能希望修改它以满足您的需求:

var shortcode = 'object';        
var $page = $('[data-shortcode-page="' + shortcode + '"]');
console.log($('[data-shortcode-page="' + shortcode + '"] :checked').length);
//after cloning the radio buttons, find radio buttons and update the name attribute.
$('.webrock-page-content').html($page.clone().find("input[type='radio']").attr("name", "test1").end().html());
console.log($('[data-shortcode-page="' + shortcode + '"] :checked').length);

更新小提琴。