复制无线电组中的值

Duplicate the value from the group of radio

本文关键字:无线电 复制      更新时间:2023-09-26

我想在其他div之前复制有源无线电Buttom的文本值。 无线电分为几组。在每个组中始终处于活动状态,只有一个。因此,每个组中的新值也应始终为 1。我将添加一个文本值,但我无法删除该组的先前值。

    $('.views-exposed-widget').find('.form-type-radio').on('click', function(){
        var a = $(this).closest('.form-type-radio').find('label[class="option"]').text();
        $('.views-widget-sort-by').before('<span>'+a+'</span>');
    });

我的例子:http://jsfiddle.net/e59ogp8a/

您可以命名它们(从而在单选组和跨度之间创建映射),以便轻松找到它们

$('.views-exposed-widget').on('change', 'input', function () {
    var self = $(this),
        name = this.name,
        text = self.closest('.form-type-radio').find('label[class="option"]').text(),
        target = $('.views-widget-sort-by').find('[data-for="'+ name +'"]'); // find the related span
    // check if we found a related span, and if not create it
    if (target.length == 0){ 
        target = $('<span data-for="'+name+'"></span>').appendTo('.views-widget-sort-by');
    }
    // set the text of the span
    target.text( text );
});

http://jsfiddle.net/gaby/5nutt42g/1/演示


您在第一个收音机上也有错误的 ID(它应该是edit-tid-1-4而不是edit-tid-1-2

您必须删除div 元素的所有上级兄弟姐妹。

$('.views-exposed-widget').find('.form-type-radio').on('click', function(){
        var a = $(this).closest('.form-type-radio').find('label[class="option"]').text();
        $('.views-widget-sort-by').prevAll().remove();
        $('.views-widget-sort-by').before('<span>'+a+'</span>');
});

您可以使用组名称作为 span 的类名,以便在单击组的按钮后清除组的值。

看看这里: http://jsfiddle.net/e59ogp8a/3/

$('.views-exposed-widget').find('.form-type-radio').on('click', function(){
    var a = $(this).closest('.form-type-radio').find('label[class="option"]').text();
    var groupName= $(this).find('.dls-radio').attr("name");
    console.log($('.views-widget-sort-by').prev('.'+groupName).text(''));            
    $('.views-widget-sort-by').before('<span class="'+groupName+'">'+a+'</span>');
});