Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQueryAjax将div替换为整页.php_Php_Jquery_Ajax_Post_Response - Fatal编程技术网

jQueryAjax将div替换为整页.php

jQueryAjax将div替换为整页.php,php,jquery,ajax,post,response,Php,Jquery,Ajax,Post,Response,我正在使用jqueryajax函数填充我的选择框。 代码如下: $(function(){ $("#maker").change(function(){ var selected_value = $("#maker").val(); $.ajax({ type: "POST", url: "search.php",

我正在使用jqueryajax函数填充我的选择框。 代码如下:

    $(function(){             
        $("#maker").change(function(){            
        var selected_value = $("#maker").val();      

        $.ajax({
            type: "POST",
            url: "search.php",
            data: ({_maker: selected_value}),
            success: function(response){
                $("#models").html(response);
            }
        });        
        });
    });
问题是这将用整个search.php页面替换div“models”!!! 我只想在“模型”中填充选项值

以下是HTML:

<div id="models">
<select id="model" name="model" style="width:170px">
<option value="Any">Any</option>
<?php
    foreach($current_models as $model) {
?>
    <option value="<?php echo $model; ?>"><?php echo $model; ?></option>

<?php
    }
?>
</select></div>

任何

因此,看起来您返回的是整个HTML页面,而不是通过搜索来查找页面的特定部分。试着这样做:

$(function(){             
    $("#maker").change(function(){            
    var selected_value = $("#maker").val();      

        $.ajax({
            type: "POST",
            dataType: "html",
            url: "search.php",
            data: ({_maker: selected_value}),
            success: function(response){
                $("#models").html($(response).find('#someDiv').html());
            }
            error: function(){
                  $("#models").html('Uh oh! Something went wrong and we weren't able to process your request correctly.');
            }
        });        
    });
});
您会注意到两件事:

  • 我指定了一个
    数据类型
    属性。这将告诉jQuery将要发生什么。另外,
    html
    数据类型告诉jQuery在将页面呈现到响应对象之前,在加载的页面上执行任何JavaScript

  • 我使用响应创建jQuery对象,然后使用jQuery的
    .find()
    方法获取响应中特定
    div
    的内部HTML

  • 下面是一个JS Fiddle的示例,为证明概念做了一些轻微的修改:


    我还想指出的是,您可能希望添加一个error属性,这样,如果页面作为错误返回,用户就知道这一点,而不是坐在那里盯着一个空白屏幕,期待发生什么事情。(我在示例中添加了一条基本错误消息,有关返回的错误属性的更多信息,请参阅)。

    发布您试图更改的html以及来自
    search.php
    search.php的示例结果?HTML?XML?JSON?您是否尝试过使用jquery.load方法?@jkinz它会以HTML形式响应