<table class="form-table">
    <tbody>
    <tr>
        <th><label for="periphery">置业顾问</label></th>
        <td>
            <div id="consultantImg">
            </div><br />
            <button type="button" class="button action" data-toggle="modal" data-target="#myConsultant" id="recConsultantBt">
                选择置业顾问
            </button>
        </td>
    </tr>
    </tbody>
</table>

<script>
    (function($){
        {% if consultant %}
            var data = {{ consultant|json_encode()}};
            $.each(data,function(i,item){
            addConsultant(item.imageUrl,item.name,item.id);
        });
        {% endif %}
        $(document).ready(function(){
            //根据上方选择的城市,选择推荐置业顾问的默认城市
            $("#recConsultantBt").click(function(){
                $('#consultCity').find('option').remove();
                $("#consultantList").find("li").remove();
                var consulCityId = $("#baseCity").val();
                var consultantName =$("#consultantName").val();
                var cityName = $("#baseCity").find("option:selected").text();
                var option = $("<option>").attr("value",consulCityId).append(cityName);
                $("#consultCity").append(option);
                searchConsultant(consulCityId,consultantName);
            });

            //添加置业顾问
            $("#consultantList").on("click",".consultantImg",function(){
                addConsultant($(this).find("img").attr("src"), $(this).text(), $(this).attr("id"));
            });
            $("#consultantImg").on("click",".consultantCancel",function(){
                $(this).parents("p").remove();
            });

            //添加置业顾问弹出层中搜索框的搜索内容
            $("#searchConsult").click(function(){
                var consulCityId = $("#consultCity").val();
                var consultantName =$("#consultantName").val();
                $("#consultantList").find("li").remove();
                searchConsultant(consulCityId,consultantName);
            });
        });
        //点击模态框中的置业顾问后显示
        function addConsultant(url,name,id){
            var img = $("<img>").attr({"src":url,"height":100,"width":100,"style":"margin-right:50px"});
            var recommendConsultant = $("<input>").attr({"type":"hidden","name":"data[recConsultant][]","value":id});
            var cancel = $("<input>").attr({"type":"button","value":"删除"}).addClass("button action consultantCancel");
            var span = $("<span>").attr("style","margin-right:50px").append(name);
            var p = $("<p>").append(img).append(span).append(cancel).append(recommendConsultant);
            $("#consultantImg").append(p);
            {% if (house_type==1 or house_type==2)%}
            controlCommand("consultantImg",1,1);
            {% endif %}
        }

    })(jQuery);

</script>