JavaScript,使用带附加的逻辑

时间:2014-10-19 21:03:38

标签: javascript jquery ajax

我有一个酒店选择列表,其中有变化的事件显示了两组不同的选项。一个是设施(复选框),另一个是房间布局(单选按钮)。

我基本上在将逻辑纳入布局列表时遇到了问题,如果设置了“isCustom”,我希望出现“已检查”状态。

我怎么能添加这个逻辑?到目前为止,我试过的任何“如果没有其他”,完全打破了追加。

HTML表单:

<label>Hotel</label>
     <select class="form-control" id="hotel_id" name="hotel_id">
        <option>Select Hotel</option>
        <option value="1">Copthorne Hotel</option>
        <option value="2">Mayfield Hotel</option>
        <option value="3">Five Mile Hotel</option>
    </select>

    <!-- Facilities  -->
    <div class="form-group" id="hide-facilities">
        <label>Facilities</label>
        <div class="row">
            <div id="facilities"></div>
        </div>
    </div>
    <!-- ./ Facilities  -->

    <!-- Layouts  -->
    <div class="form-group" id="hide-layouts">
        <label>Layouts</label>
        <div class="row">
            <div id="layouts"></div>
        </div>
    </div>

的JavaScript

<script>
  $(document).ready(function($){

        // on selection of hotel 
        $('#hotel_id').change(function(){
            $("#gethotel_id").val($('#hotel_id').val());

            //populate options for facilities
            $.getJSON("api/facilitieslist", { option: $(this).val() }, 
            function(data) {
                $('#hide-facilities').show();
                if ( data.success == false ) { 
                     $('#facilities').empty();
                     $('#facilities').append('<div>No facilities associated with this room</div>');
                } else {
                    $('#facilities').empty();
                    $.each(data, function(key, value) { 
                         $('#facilities').append('<div><input type="checkbox" value="' + value.id +'" name="facilities[]"> ' + value.name +'</div>');
                    });

                }
            });

            //populate options for layouts
            $.getJSON("/api/layoutslist", { option: $(this).val() }, 
            function(data) {
                $('#hide-layouts').show();
                if ( data.success == false ) { 
                     $('#layouts').empty();
                     $('#layouts').append('<div>No layouts associated with this hotel</div>');
                } else {
                    $('#layouts').empty();
                        $.each(data, function(key, value) { 
                            var isCustom = (value.title === 'Custom'); // logic should relate to this
                            $('#layouts').append('<div><img src="/assets/img/rooms/' + value.image_name +'" alt="'+ value.title +'"><div class="caption"><p><input type="radio" value="' + value.id +'" name="layout"> ' +  value.title +'</p></div></div>');



                        });
                }
            });

        });


    }); // end document ready

0 个答案:

没有答案