<div id="preview">
    <table class="form-table">
        <tbody>
        <tr>
            <th style="width:11.7%"><label for="traffic" style="margin-left: 30px">主力房源:</label></th>
            <td>
                <input type="file" name="files[0]" property="0" class = "files form-control"multiple style="width: 30%;">
            </td>
        </tr>
        </tbody>
    </table>
    {% set exists_ids = "" %}
    {% for item in mainImage %}
    {% if exists_ids != "" %}
    {% set exists_ids = exists_ids~"," %}
    {% endif %}
    {% set exists_ids = exists_ids~item.id %}
    <div>
        <img src="{{siteUrl}}{{item.path}}" height="90" width="140" style="margin-right: 50px;margin-top:10px">
        <select name="exists[{{item.id}}][buildProperty]" style="margin-right: 50px;width: 10%;display: inline-block" class="form-control">
            {% for i in buildProperty %}
            <option {{ i.id == item.buildproperty_id?"selected":"" }} value="{{i.id}}">{{i.value}}</option>
            {% endfor %}
        </select>
        <input type="text" placeholder="面积" name="exists[{{item.id}}][housearea]" class="form-control" value="{{item.area}}" style="width: 100px;display:inline-block; margin-right: 50px">
        <input type="button" value="删除" class="button action cancel existsCancel" style="margin-top: 30px">
    </div>
    {% endfor %}

    <p></p>
    <input type="hidden" name="exists_ids" value="{{exists_ids}}" >


</div>

<script>
    (function($){
        $(document).ready(function(){
            $("form").on("change",".files",function(){
                mainHouse(this,i);
                $(this).hide();
                i++;
            });

            $("#preview").on("click",".cancel",function(){
              $(this).parent("div").remove();
            });
        });
        //显示主力户型
        function mainHouse(input,i){
            var reader = new FileReader();
            reader.onload = function (e) {
                var img = $("<img>").attr({"id":"target","src":e.target.result,"height":90,"width":140,"style":"margin-right:50px;margin-top:10px;"});
                var button = $("<input>").attr({"type":"button","value":"删除","property":+i,"id":+i}).addClass("button action cancel");
                var type = $("<input>").attr({"type":"hidden","name":"data["+i+"][type]","value":4,"property":+i});
                var mainHousePic = $("<input>").attr({"type":"hidden","name":"data["+i+"][mainHouse]","value":0,"property":+i});
                var file = $("<input>").attr({"type":"file","name":"files["+(i+1)+"]","property":+(i+1)}).addClass("files form-control").css("width","20%");
                var select = $("<select>").attr({"name":"data["+i+"][buildProperty]","style":"margin-right:50px;width:10%;display:inline-block"}).addClass("form-control");
                {% for item in buildProperty %}
                    select.append($("<option>").attr("value",{{item.id}}).append('{{item.value}}'));
                {% endfor%}
                var areatext = $("<input>").attr({"type":"text","placeholder":"面积","name":"data["+i+"][housearea]","style":"width: 100px;display:inline-block;margin-right:50px"}).addClass("form-control");
                var div = $("<div>").attr({"property":+i}).append(img).append(select).append(areatext).append(button).append(type).append(mainHousePic);
                $("form").find("#preview > p").after(file);
                $("#preview > p").append(div);

            }
            reader.readAsDataURL(input.files[0]);
        }
    })(jQuery);
</script>