jQuery ajax仅序列化2个字段 [英] jQuery ajax serialize only 2 fields

查看:65
本文介绍了jQuery ajax仅序列化2个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何通过更改以下行来序列化整个表单,如下面的示例或表单的一个特定字段:

I know how to serialize the whole form like in example below or one specific field of the form by just changing the line:

data: $('form').serialize(),

data: $('#input-field').serialize(),

.

$(document).on('input paste', '#soap', function () {
  $.ajax({
     type:'POST',
     url:'/soap',
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     data: $('form').serialize(),
         success:function(data){
            $('input[id=msg]').val(data.msg);
         }
    });
});

但是我想序列化具有ID的2个输入字段....如何做到这一点?

But i want to serialize 2 input fields that have an id....how to do that?

推荐答案

方法1:通过逗号分隔的选择器,您可以使用以下代码将两个字段一起序列化

Way 1: By comma separated selector you can serialize two fields together using following code

var seializedTwoFields = $('#input-field1,#input-field2').serialize();

方法2:当您使用jQuery serialize()函数时,它只是将您的字段转换为a = 1格式的字符串.因此,您当然可以将此函数应用于两个字段,并使用&将其连接起来.他们之间.

Way 2: When you use the jQuery serialize() function, it simply turns your field into a string in the format a=1. So you can certainly apply this function to two fields and concatenate the result, with an & between them.

请尝试使用此代码段.

var formfield1 = $('#input-field1').serialize();
var formfield2 = $('#input-field2').serialize();

var seializedTwoFields = formfield1+'&'+formfield2;

这篇关于jQuery ajax仅序列化2个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆