简体   繁体   English

使用Ajax基于2个输入字段的jQuery自动完成

[英]jquery autocomplete based on 2 input fields using ajax

I have a form with 2 input fields. 我有一个带有2个输入字段的表单。 I would like to implement autocomplete on input 2 using input 1 as a filter. 我想使用输入1作为过滤器在输入2上实现自动完成功能。 so i need to pass both arguments to my cgi script. 所以我需要将两个参数都传递给我的cgi脚本。 i am having issues doing so. 我这样做有问题。 i can pass them individually but not a the same time. 我可以单独通过它们,但不能同时通过。 here what i tried: 这是我尝试的:

function fillbox2(){                                                          

$('#input2').autocomplete({                                              
      source: function(request, response ){                                                               
      var frmStr =  {input1:$('#input1').val(),input2:$('#input2').val()};                                                                     
      $.ajax({                                                            
      url: './cgi_temp2.cgi',                                             
      dataType: 'json',                                                   
      data:{frmStr:request.term},                                      
      contentType: "application/json; charset=utf-8",                     

          success: function (data) {                                      
               response ($.map( data.matches, function(item){             
                           return {                                       
                              value: item.info2,                    

                           }                                              
                       }));                                               
              }                                                           
          });                                                             
      },                                                                  

          minLength: 2,                                                   
          select: function(event, ui){                                    
          $("#input2).val(ui.item.value);                             
          return false;                                                   
          }                                                               
  });                                                                     

}       

where is the issue? 问题出在哪里? is it from "data:" in my ajax call or in "success:"? 是来自我的ajax调用中的“数据:”还是“成功:”中的?

As i can see you save data from input in your "frmStr" but in your ajax request, you do that data:{frmStr:request.term}, this mean you are using your variable "frmStr" as key in data and on your back-end you will have frmStr as key . 正如我看到的那样,您可以从“ frmStr”中的输入中保存数据,但是在ajax请求中,您可以执行以下数据:{frmStr:request.term},这意味着您将变量“ frmStr”用作数据中和数据中的键在后端, 您将把frmStr作为key Your data in ajax should look something like that 您在ajax中的数据应如下所示

var frmStr =  {
    input1:$('#input1').val(),
    input2:$('#input2').val(),
    requestTerm: request.term
};

and then in ajax 然后在ajax中

data:{data: frmStr},

That should work to send data to back-end 应该可以将数据发送到后端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM