简体   繁体   English

在jQuery ajax中传递多个输入字段的值?

[英]Pass value of multiple input fields in jQuery ajax?

I have multiple input fields, like 我有多个输入字段,例如

  <p>Filter by gender</p>
  <select class="filter-users">
     <option value="female">Female</option>
     <option value="male">Male</option>
   </select>
   <p>Filter by city</p>
      <input class="filter-users" type="text" name="city">

How to create GET request url with these one or all of these inputs 如何使用这些输入中的一个或全部创建GET请求网址

$.ajax({
  url: "/users/filter",
  type: "get", 
  data: { 
    city: city, 
    gender: gender
  },
  success: function(response) {

  }
});

How to make ajax request every time on of the filter inputs changed? 每次更改过滤器输入时,如何发出Ajax请求? For example send new request when on option change, or text input was entered? 例如,在更改选项时发送新请求,或者输入了文本输入? So what I am trying to explain is, there is no submit button. 所以我想解释的是,没有提交按钮。 Requests needs to be made everytime one of these fields changes 每当这些字段之一发生更改时,都需要发出请求

You can use serializeArray , like this: 您可以使用serializeArray ,如下所示:

$.ajax({
  url: "/users/filter",
  type: "get", 
  data: $("form").serializeArray(),
  success: function(response) {
  }
});

Or you also can use serialize in case you don't need JSON but URL-encoded parameters. 或者,如果不需要JSON,但需要URL编码的参数,也可以使用serialize

for this "How to make ajax request every time on of the filter inputs changed"- 为此,“如何每次更改过滤器输入时都发出ajax请求”-

$('input.filter-users').on('change', function(){
   $.ajax({
     url: "/users/filter",
     type: "get", 
    data: $("form").serializeArray(),
    success: function(response) {
       }
   });
});

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

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