How can I unify text that take from multiple group radio button?

后端 未结 3 1393
再見小時候
再見小時候 2020-12-04 03:54

My javascript code like this :

$(function(){
    $(\'input[type=\"radio\"]\').click(function(){
        var txt = $(this).parent().text();
        var $radio         


        
3条回答
  •  自闭症患者
    2020-12-04 04:39

    You need to first get the text value of the result : var str = $('#result-select').text();, then when you check a radio button, you just append the value to the result text str += txt; $('#result-select').text(str);.

    And if you uncheck a radio button, you just replace it's value with an empty string str = str.replace(txt,'');.

    $(function(){
        $('input[type="radio"]').click(function(){
            var txt = $(this).parent().text();
            var $radio = $(this);
    
            // if this was previously checked
            if ($radio.data('waschecked') == true)
            {
                $radio.prop('checked', false);
                $radio.data('waschecked', false);
                var str = $('#result-select').text();
                str = str.replace(txt,'');
                $('#result-select').text(str);
            }
            else{
                $radio.data('waschecked', true);
                var str = $('#result-select').text();
                str += txt;
                $('#result-select').text(str);
            }
    
            // remove was checked from other radios
            $radio.siblings('input[type="radio"]').data('waschecked', false);
        });
    });
    

提交回复
热议问题