簡體   English   中英

在JSON的響應對象上調用Ajax

[英]Call Ajax on the response object of JSON

我正在使用json的ajax調用。 我從ajax電話打電話給酒店列表,但現在我必須打電話給特定酒店的價格。 請支持。

酒店列表代碼如下:
HTML

<td style="color: #993300"><strong>Add Services<input name="add-service" id="add-service" type="button" value="+" style="background-color: #993300; color: #FFFFFF;" /></strong></td>

JQuery用於調用ajax獲取酒店列表:

$('#cmb_service').bind('change', function(){

        var value = $(this).val();
        var destination = $( "#destination" )
        $.ajax({
                type : 'POST',
                url : '../enquiries/getpricebyajax',
                dataType : 'json',
                data: {
                        service : value,
                        destno : destination.val()
                },
                success : function(data) {
                       $('#waiting').hide(500);
                       $('#divserviceprovider').text('');
                       $('#divserviceprovider').append(data.msg);
                       $('#divserviceprovider').show(500);
                       if (data.error === true)
                            $('#divserviceprovider').show(500);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                        $('#waiting').hide(500);
                        $('#divserviceprovider').removeClass().addClass('error')
                            .text('There was an error.').show(500);
                        $('#divserviceprovider').show(500);
                }
        });
        return false;
});`

酒店列表響應的PHP代碼如下:

function getpricebyajax()

{
            $str="";$substr="";
            if(!empty($_POST['service']))
                {
                    switch ($_POST['service']) {
                        case "3":
                            {
                                $rshotels=$this->Enquiry->query("SELECT id, name FROM hotels where destination_id=".$_POST['destno']);
                                foreach($rshotels as $hotel){
                                        $substr.='<option value="'.$hotel['hotels']['id'].'">'.$hotel['hotels']['name'].'</option>';
                                }
                                $str.= '<select id="cmb_hotel" name="cmb_hotel">'.$substr.'</select>';
                                $str.= '<div id="divhotel_details"></div>';
                            }
                            break;
                        default:
                            break;
                    }
                    $return['error'] = true;
                    $return['msg'] = $str;
                }
            exit(json_encode($return));
    }

`

我直接在div中粘貼html的代碼。 它很好地顯示了酒店列表。 但是選擇“divhotel_details”的代碼是什么。 當我點擊divhotel_details時,我必須再次調用ajax來生成該酒店的價格。

請建議我。

提前致謝。

你可以在這里檢查一下調用json服務的正確方法:) - https://rvieiraweb.wordpress.com/2013/01/21/consuming-webservice-net-json-using-jquery/

編輯:

<script>

function AjaxCall(){
  var hotel_val = $("#ddl_hotel").val();

   //do service ajax call passing the hotel val 

   success: function(response) {
     $("#display_info").empty();
     //this 
     $("#display_info").append(response.Yourfields);
     //or LOOP and show in div
   }, 
   error: function(response) {
       $("#display_info").append("No info for this hotel");
   }
}
</script>

<select id="ddl_hotel" onchange="AjaxCall();">
  <option value="hotel1">Hotel 1</option>
  <option value="hotel2">Hotel 2</option>
  <option value="hotel3">Hotel 3</option>
</select>

<div id="display_info">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM