请问如何把下面代码中相同的代码或逻辑,提取出来,用时在放进去,不用重复写。要详细代码奥 80C

if(isAdd) { //添加奖品
var _html = "

";
_html += '' + $("input[name='prize_name']").val() + ''; // 名称
_html += '' + $("select[name='prize_type']").find("option:selected").html() + '' + $("select[name='prize_type']").val() + ''; //类型
//奖品图片
_html += '' + $("input[name='prize_img']").val() + '';
    //奖品设置  添加
    var type_val = $("select[name='prize_type']").val();
    if(type_val == "1"){
        _html += '<td class="prizeContent"><em>' + $("input[name='donate_scores']").val() + '</em><span></span></td>';
    }else if(type_val == "2"){
        var package_val = $("input[name='red_package']:checked").val();
        var scene_title = $('#red_packet_scene').find("option:selected").text();
        var scene = $('#red_packet_scene').val();
        if(!scene){
            scene_title = '';
        }

        if(package_val == "0"){
            _html += '<td class="prizeContent" data-scene="' + scene + '" data-red_name="' + $("input[name='red_name']").val() + '" data-red_wish="' + $("input[name='red_wish']").val() + '">固定红包[' + scene_title + ']:<em class="fixed_money">' + $("input[name='fixed_money']").val() + '</em>元<span>' + package_val + '</span></td>';
        }else if(package_val == "1"){
            _html += '<td class="prizeContent" data-scene="' + scene + '" data-red_name="' + $("input[name='red_name']").val() + '" data-red_wish="' + $("input[name='red_wish']").val() + '">随机红包[' + scene_title + ']:<em class="rand_min">' + $("input[name='rand_min']").val() + '</em>-<em class="rand_max">' + $("input[name='rand_max']").val() + '</em>元<span>' + package_val + '</span></td>';
        }
    }else if(type_val == "3"){
        var get_type = $("select[name='get_type']").val();
        if(get_type == "1") { //快递
            _html += '<td class="prizeContent"><em>' + $("input[name='goods_name']").val() + '</em><span>' + $("select[name='get_type']").val() + '</span><b></b><big></big></td>';
        }else{ //门店
            var store_id = '';
            if($('input[name="get_point"]:checked').val() == 2){
                if($('input[name="store_id"]:checked').length > 0){
                    $.each($('input[name="store_id"]:checked'), function(){
                        store_id += $(this).val() + ',';
                    });
                }else{
                    layer.msg('请选择门店!',{'icon': 5});
                    return;
                }
            }

            _html += '<td class="prizeContent"><em>' + $("input[name='goods_name']").val() + '</em><span>' + $("select[name='get_type']").val() + '</span><b>' + $('input[name="get_point"]:checked').val() +'</b><big>'+store_id +'</big></td>';

        }
    }else if(type_val == "4"){
        if($("select[name='package']").val() != "0"){
            var goods_name = $(".youhui .select2-chosen").text();
            var _index = $(".youhui .select2-chosen").text().indexOf("库存:");
            goods_name = $(".youhui .select2-chosen").text().substring(0, _index);
            _html += '<td class="prizeContent"><em>' + goods_name + '</em><span>' + $("select[name='package']").val() + '</td>';
        }else{
            return false;
        }
    }else if(type_val == "5"){
        if($("select[name='free_voucher']").val() != "0"){
            var serial_number = $(".tzmdj .select2-chosen").text();
            var _index = $(".tzmdj .select2-chosen").text().indexOf("库存:");
            serial_number = $(".tzmdj .select2-chosen").text().substring(0, _index);
            _html += '<td class="prizeContent"><em>' + serial_number + '</em><span>' + $("select[name='free_voucher']").val() + '</td>';
        }else{
            return false;
        }
    }

    _html += '<td class="prizeNums">' + $("input[name='nums']").val() + '</td>';
    _html += '<td class="prizeChance">' + $("input[name='chance']").val() + '</td>';
    _html += '<td class=""><a href="javascript:void(0)" class="edit" >编辑</a>  <a href="javascript:void(0)" class="del_tr" >删除</a> </td></tr>';

    $(".prize_msg").append(_html);
    $(".prize_setting").find("input").not("input[type='radio']").not(".red_text").not('.object_expired_day').val("");
    $('.temp_img').attr('src', '');
}else{ //保存设置
    $(_target).closest("tr").find("td:first").html($("input[name='prize_name'").val());
    $(_target).closest("tr").find("td.prizeType").find("em").html($("select[name='prize_type']").find("option:selected").html());
    $(_target).closest("tr").find("td.prizeType").find("span").html($("select[name='prize_type']").val());
    $(_target).closest("tr").find("td.prize_img").find("span").html($("input[name='prize_img']").val());

    //奖品设置  修改
    var type_val = $("select[name='prize_type']").val();
    if(type_val == "1"){
        $(_target).closest("tr").find("td.prizeContent").html("<em>" + $("input[name='donate_scores']").val() + "</em><span></span>");
    }else if(type_val == "2"){
        var red_val = $("input[name='red_package']:checked").val();
        var scene_title = $('#red_packet_scene').find("option:selected").text();
        var scene = $('#red_packet_scene').val();
        if(!scene){
            scene_title = '';
        }
        if(red_val == "0"){
            var red_html = "固定红包[" + scene_title + "]:<em>" + $("input[name='fixed_money']").val() + "</em>元<span>" + red_val + "</span>";
            $(_target).closest("tr").find("td.prizeContent").data("red_name", $("input[name='red_name']").val()).data("red_wish", $("input[name='red_wish']").val()).data('scene', scene).html(red_html);
        }else{
            var red_html = "随机红包[" + scene_title + "]:<em class='rand_min'>" + $("input[name='rand_min']").val() + "</em>-<em class='rand_max'>" + $("input[name='rand_max']").val() + "</em>元<span>" + red_val + "</span>";
            $(_target).closest("tr").find("td.prizeContent").data("red_name", $("input[name='red_name']").val()).data("red_wish", $("input[name='red_wish']").val()).data('scene', scene).html(red_html);
        }
    }else if(type_val == "3"){
        if($('select[name="get_type"]').val() == 1){
            $(_target).closest("tr").find("td.prizeContent").html("<em>" + $("input[name='goods_name']").val() + "</em><span>" + $("select[name='get_type']").val() + "</span>");
        }
        if($('select[name="get_type"]').val() == 2){
            if($('input[name="get_point"]:checked').val() == 1){
                $(_target).closest("tr").find("td.prizeContent").html("<em>" + $("input[name='goods_name']").val() + "</em><span>" + $("select[name='get_type']").val() + "</span><b>" + $('input[name="get_point"]:checked').val() + '</b><big></big>');
            }
            if($('input[name="get_point"]:checked').val() == 2){
                var store_id = '';
                if($('input[name="get_point"]:checked').val() == 2){
                    if($('input[name="store_id"]:checked').length > 0){
                        $.each($('input[name="store_id"]:checked'), function(){
                            store_id += $(this).val() + ',';
                        });
                    }else{
                        layer.msg('请选择门店!',{'icon':5});
                        return;
                    }
                }
                $(_target).closest("tr").find("td.prizeContent").html("<em>" + $("input[name='goods_name']").val() + "</em><span>" + $("select[name='get_type']").val() + "</span><b>" + $('input[name="get_point"]:checked').val() + '</b><big>' + store_id + '</big>');
            }
        }
    }else if(type_val == "4"){
        if($("select[name='package']").val() != "0"){
            var goods_name = $(".youhui .select2-chosen").text();
            var _index = $(".youhui .select2-chosen").text().indexOf("库存:");
            goods_name = $(".youhui .select2-chosen").text().substring(0, _index);
            var hui_html = "<em>" + goods_name + "</em><span>" + $("select[name='package']").val() + "</span>";
            $(_target).closest("tr").find("td.prizeContent").html(hui_html);
        }else{
            return false;
        }
    }else if(type_val == "5"){
        if($("select[name='free_voucher']").val() != "0"){
            var serial_number = $(".tzmdj .select2-chosen").text();
            var _index = $(".tzmdj .select2-chosen").text().indexOf("库存:");
            serial_number = $(".tzmdj .select2-chosen").text().substring(0, _index);
            var tzmdj_html = "<em>" + serial_number + "</em><span>" + $("select[name='free_voucher']").val() + "</span>";
            $(_target).closest("tr").find("td.prizeContent").html(tzmdj_html);
        }else{
            return false;
        }
    }

    $(_target).closest("tr").find("td.prizeNums").html($("input[name='nums']").val());
    $(_target).closest("tr").find("td.prizeChance").html($("input[name='chance']").val());
    isAdd = true;
    $(".add-button").find("button").html("添加奖品");
    $(".prize_setting").find("input").not("input[type='radio']").not(".red_text").not('.object_expired_day').val("");
    $('.temp_img').attr('src', '');
}

});

1个回答

找共同特征啊,比如1、2、3,你写了三个这样的,你可以直接写一个函数就是创建span标签,把文本值设置为函数参数,每次再创建直接调用这个方法即可。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问