简体   繁体   English

如何使用ajax传递多个值?

[英]How to pass multiple values with ajax?

I'm looking to pass multiple values with ajax. 我想通过ajax传递多个值。

My Code: 我的代码:

$(".ajax-button").change(function(event){

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            }
        });

        $.ajax({
            url:'{{action("WebshopController@refreshCheckout")}}' ,
            data: {
                frame: $('input.frame:checked').val(),
                color: $('input.color:checked').val()
            },
            method: 'GET'
        }).done(function(response){
            console.log(response);
        });
    });

As you can see im trying to send a frame and a color: 如您所见,即时通讯正在尝试发送框架和颜色:

data: {
                frame: $('input.frame:checked').val(),
                color: $('input.color:checked').val()
            },

However it does not combine these 2, when I click the checkbox for frame it only sends the frame: and when I click checkbox for color is only sends the color. 但是,它没有合并这2个元素,当我单击框架复选框时,它仅发送框架:而当我单击颜色复选框时,仅发送颜色。 as seen in the printscreen below. 如下面的打印屏幕所示。

在此处输入图片说明

I want it to build the URL like: refreshCheckout?color=Silver&frame=h35 我希望它构建如下网址:refreshCheckout?color = Silver&frame = h35

How do I achieve this? 我该如何实现?

Help is appreciated. 感谢帮助。

If a value is undefined...jQuery won't include that property. 如果未定义值,则jQuery将不包含该属性。 You can't get a value from a selector that doesn't exist (not checked) 您无法从不存在的选择器中获得一个值(未选中)

You probably want to make sure you have both before submitting: 您可能要确保在提交之前都拥有两者:

$(".ajax-button").change(function(event) {

  var $frameInput = $('input.frame:checked'),
    $colorInput = $('input.color:checked');

  if (!($frameInput.length && $colorInput.length)) {

    alert('Need both color and frame');

  } else {

    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': '{{ csrf_token() }}'
      }
    });

    $.ajax({
      url: '{{action("WebshopController@refreshCheckout")}}',
      data: {
        frame: $frameInput.val(),
        color: $colorInput.val()
      },
      method: 'GET'
    }).done(function(response) {
      console.log(response);
    });
  }
});

you can also send data like 您还可以发送数据

$(".ajax-button").change(function(event){

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': '{{ csrf_token() }}'
        }
    });

    $.ajax({
        url:'{{action("WebshopController@refreshCheckout")}}/?frame=$("input.frame:checked").val()&color=$("input.color:checked").val()' ,
        method: 'GET'
    }).done(function(response){
        console.log(response);
    });
});

编辑: 下面的答案应该是被接受的。

I suppose following expressions return nothing 我想以下表达式什么都不返回

frame: $('input.frame:checked').val(),
                color: $('input.color:checked').val()

Check it before the request. 在请求之前检查它。 It will work if you pass some data there 如果您在那里传递一些数据,它将起作用

data: {
                frame: 'foo',
                color: 'bar'
            },

You didn't post your HTML code, so it's hard to tell what exactly is broken. 您没有发布HTML代码,因此很难说出到底是什么。 However, simply to illustrate that the current accepted answer is probably not what you are looking for and that your code is correct except that you are likely not triggering the event when you really want to (ie when both fields are set) as suggested in the answer by charlietfl. 但是,只是为了说明当前接受的答案可能不是您要查找的内容,并且您的代码是正确的,除了在您确实希望(如同时设置两个字段)时,您可能不触发事件,如charlietfl的回答。

https://jsfiddle.net/b1g1pmnp/ https://jsfiddle.net/b1g1pmnp/

HTML: HTML:

  <p>
  Color
  </p>
  <input type="radio" class="color" value="red" name="color">
  red
  <input type="radio" class="color" value="orange" name="color">
  orange
  <input type="radio" class="color" value="purple" name="color">
  purple
  <input type="radio" class="color" value="green" name="color">
  green

  <p>
  Frame
  </p>
  <input type="radio" class="frame" value="silver" name="frame">
  silver
  <input type="radio" class="frame" value="gold" name="frame">
  gold
  <input type="radio" class="frame" value="tan" name="frame">
  tan
  <input type="radio" class="frame" value="black" name="frame">
  black

  <p><button class="ajax-button">Button</button></p>

JS: JS:

    $(".ajax-button").click(function(event) {
     // OP code         
     var color = $('input.color:checked').val();
     var frame = $('input.frame:checked').val();

     console.log("Current OP Code Results: ");
     console.log(color);
     console.log(frame);

     // Accepted code
      var data1 = $('input.color').val();
      var data2 = $('input.frame').val();

      console.log("Current Accepted Answer Results: ");
      console.log(data1);
      console.log(data2);

      return false;
    });

You can play around with that and see what you get for various cases. 您可以尝试一下,看看各种情况下会得到什么。

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

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