<table class="form-table">
    <tbody>
    <tr>
        <th><label for="feature">房源特色</label></th>
        <td>
            <div id="feature" class="row">
            </div><br/>
            <button type="button" class="button action" id="addFeatureBtn">
                添加特色
            </button>
        </td>
    </tr>
    </tbody>
</table>

<script>
    $(document).ready(function () {
        {% set featureOld = function('SearchDao::searchFeatureOld', houseId, 1)|json_encode() %}
        {% if featureOld %}
        var data = {{ featureOld }};
        $.each(data, function (i, item) {
            addFeature(item.tag_id, item.name);
        });
        {% endif %}

        var featureArray = [];
        $('#addFeatureBtn').click(function(){
            init_modal_addTag(1, function (id, value) {
                featureArray = [];
                $('input[name="data[houseFeature][]"]').each(function (index, item) {
                    featureArray.push(Number($(item).val()));
                });
                if(featureArray.indexOf(Number(id)) == -1){
                    addFeature(id, value);
                }else{
                    alert('不能重复选择特色');
                }
            });
        });
    });
    function addFeature(id, value) {
        var houseFeature = $("<input>").attr({"type": "hidden", "name": "data[houseFeature][]", "value": id});
        var cancel = $("<span>").attr("aria-hidden", "true").addClass("glyphicon glyphicon-remove featureCancel ");
        var span = $("<span>").attr("style", "margin-right:5px").append(value);
        var divCol = $("<div>").append(houseFeature).append(span).append(cancel).addClass("col-md-3").attr("style", "margin-bottom:10px");
        $("#feature").append(divCol);
    }
</script>