jQuery封装数组或集合使用Ajax提交

jQuery封装数组使用Ajax提交

jQuery中实现多选

			<input id="btn_delete" class="btn btn-danger" value="删除">
			<table class="table table-hover" id="product_table">
                <thead>
                    <tr>
                    	<th>
                            <input id="check_all" type="checkbox"/>
                        </th>
                    </tr>
                </thead>
                <tbody>
					<tr>
						<td>
							<input class="check_id" type="checkbox">
						</td>
					</tr>
                </tbody>
            </table>



<script type="text/javascript">

	//全选
	$("#check_all").click(function(){
		$(".check_id").prop("checked",$(this).prop("checked"));
	});

	//反选  实现当复选框都选中全选框也默认选中
	$(document).on("click",".check_id",function(){
		var flag = $(".check_id:checked").length==$(".check_id").length;
		$("#check_all").prop("checked",flag);
	})
</script>

批删
方法一

	$("#btn_delete").click(function(){
		//Jquery得到数组
		var ids = [];
		//遍历选中的
		$.each($(".check_id:checked"),function(){
			ids.push($(this).val());//将选中的值放到数组中
		});
		if(confirm("确定删除吗?")){
			//使用ajax提交
			$.post(
				"deleteByIds",
				{"ids":ids},
				function(result){
					alert(result.msg);
				}
			)
		}
	});





后台controller层使用json进行接收
	@ResponseBody
	@RequestMapping(value = "deleteByIds",method = RequestMethod.POST)
	public ResultTO deleteProduct(@RequestParam(value = "ids[]") Integer[] ids) {
		ResultTO resultTO = null;
		try {
			resultTO = productService.deleteProduct(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultTO.newFailResultTO("删除失败", null);
		}
		return resultTO;
	}
	//一定要使用这样的注解@RequestParam(value = "ids[]")不然会接收不到值

方法二

		var ids = [];
         $.each($(".check_id:checked"),function () {
        	 ids.push($(this).val()); 
         });
    	$.ajax({
    		type: 'post',
    		dataType: 'json',
    		url: 'deleteByIds',
    		data: {ids:ids},
    		traditional: true,
    		success: function (result) {
    			alert(result.msg);
    		},
    		error: function(data){
    		  alert("操作异常");
    		}
    	});





后台controller层使用json进行接收
	@ResponseBody
	@RequestMapping(value = "deleteByIds",method = RequestMethod.POST)
	public ResultTO deleteProduct(Integer[] ids) {
		ResultTO resultTO = null;
		try {
			resultTO = productService.deleteProduct(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultTO.newFailResultTO("删除失败", null);
		}
		return resultTO;
	}

方法三

		var ids = [];
         $.each($(".check_id:checked"),function () {
     		ids.push($(this).val()); 
         });
      		$.ajax({
      			type: 'post',
      			dataType: 'json',
      			url: 'deleteByIds',
      			data: JSON.stringify(ids),
      			contentType:"application/json",
      			success: function (result) {
      				alert(result.msg);
      			},
      			error: function(data){
      			  alert("操作异常");
      			}
      		});

后台controller层使用json进行接收
	@ResponseBody
	@RequestMapping(value = "deleteByIds",method = RequestMethod.POST)
	public ResultTO deleteProduct(@RequestBody List<Integer> ids) {
		ResultTO resultTO = null;
		try {
			Integer[] idsa = ids.toArray(new Integer[0]);
			resultTO = productService.deleteProduct(idsa);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultTO.newFailResultTO("删除失败", null);
		}
		return resultTO;
	}

方法四

	$("#btn_delete").click(function(){
		var ids = $(".check_id:checked").map(function(){
            return this.value;
         }).get().join(",");
		if(confirm("确定删除吗?")){
			//使用ajax提交
			$.post(
				"deleteByIds",
				{"ids":ids},
				function(result){
					alert(result.msg);
				}
			)
		}
	});





后台controller层使用json进行接收
	@ResponseBody
	@RequestMapping(value = "deleteByIds",method = RequestMethod.POST)
	public ResultTO deleteProduct(Integer[] ids) {
		ResultTO resultTO = null;
		try {
			resultTO = productService.deleteProduct(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultTO.newFailResultTO("删除失败", null);
		}
		return resultTO;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值