使用AJAX传数组,后台接受数组

xiaoxiao2021-02-28  51

1.前台界面,获取相同input中的值

例如有3个相同的input,我在后台要获取这三个input里面的值

<input type="text" name="starry">

<input type="text" name="starry">

<input type="text" name="starry">

2.js代码

首先获取这几个input:

var  starry = $("input[name='starry']");

使用$.map() 函数用于使用指定函数处理数组中的每个元素(或对象的每个属性),并将处理结果封装为新的数组返回:

var arr = $.map(starry,function(item,index){

return item.value;

})

使用ajax将值传入后台:(traditional:true这个属性很重要

$.ajax({

type:'post',

url:'option/updateseq',

data:{arrs:arr},

traditional:true,

success:function(data){

alert('更新成功');

}

})

3.后台获取前台传入的数组

@RequestMapping("updateseq")

@ResponseBody

public RespModelupdateseq(String[] arrs){

//执行逻辑代码

return RespModel;

}

转载请注明原文地址: https://www.6miu.com/read-1699997.html

最新回复(0)