代码之家  ›  专栏  ›  技术社区  ›  Inception

将Ajax请求的JSON结果存储到一个javascript变量中,以便于自动完成

  •  1
  • Inception  · 技术社区  · 6 年前

    customerid productname 使用easyautocomplete插件。

    $('#customerid').on('change',function() {
        var products2 = {
              url: function(phrase) {
                return "database/FetchCustomerProducts.php";
            },  
            ajaxSettings: {
                dataType: "json",
                method: "POST",
                data: {
                    dataType: "json"
                }
            },
            preparePostData: function(data) {
                data.phrase = $("#customerid").val();
                return data;
              },
            getValue: "name",
            list: {
                onSelectItemEvent: function() {
                    var value = $("#productname").getSelectedItemData().id;
                    $("#productid").val(value).trigger("change");
                },
                match: {
                  enabled: true
                }
            }
        };
    
    $("#productname").easyAutocomplete(products2);
    });
    

    if(!empty($_POST["customerid"])){
    
        $products = $app['database']->Select('products', 'customerid', $_POST['customerid']);
    
        echo json_encode(['data' => $products]);
    
    }
    

    this

    1 回复  |  直到 6 年前
        1
  •  3
  •   Inception    6 年前

    您可以使用element select category add is class“check_catogory”

    $("#customerid").change(function(){
        var id = $(this).val();
        if(id!=""){
            $.ajax({
                url:"database/FetchCustomerProducts.php",
                type:"POST",
                data:{id:id},
                dataType: "json",
                cache:false,
                success:function(result){
                     var options = {
                        data:result,
                        getValue: "name",
                        list: {
    
                            onSelectItemEvent: function() {
                                var value = $("#productname").getSelectedItemData().id;
                                $("#productid").val(value).trigger("change");
                            },
    
                            match: {
    
                              enabled: true
    
                            }
    
                        }
                    };
                    $("#productname").easyAutocomplete(options);
    
                }
            })
        }
    });
    
    推荐文章