如何通过ajax/jquery在spring控制器中获取复选框值? [英] how do i get the checkbox value in spring controller through ajax/jquery?

查看:74
本文介绍了如何通过ajax/jquery在spring控制器中获取复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSP中,我是这样的,

In JSP i have like this,

<form:checkbox path="wdays" value="${day.getDaysId()}"/>

和jQuery类似

var wday = ($('input:checkbox:checked').val());
$.ajax({
    type: "POST",
    url: contexPath + "/saveShiftAllotment",
    data: "&wday="+ $('input:checkbox:checked').val(),
    success: function(data)
    {       
        alert("Submited to the Medical Department");
    }

和我的控制器类似,

public void
saveShiftAllotment(@RequestParam(@RequestParam(value="wday") int wday)

推荐答案

在您的问题中,无论是否选中,都不清楚是否只想获取该值.所以我假设您触发了ajax调用选中该复选框时.渲染后, <form:checkbox path="wdays" value="${day.getDaysId()}"/> 会生成与此类似的内容.

In your question, you were not clear if you just want to grab the value, no matter if its checked or not.So i assume you triggering your ajax call when the check box is checked. <form:checkbox path="wdays" value="${day.getDaysId()}"/> will generate something similar to this when rendered.

<input type="checkbox" id="wdays" name="wdays" value="5"/>

因此,您可以使用

获取价值.

$('#wdays').val()

检查是否已选中

$('#wdays').is(":checked")

您的ajax通话将会

$.ajax({
    type: "POST",
    url: contexPath + "/saveShiftAllotment",
    data: {wday:$('#wdays').val()},
    success: function(data)
    {       
        alert("Submited to the Medical Department");
    }

在弹簧控制器侧,执行以下操作.

On your spring controller side, do the below.

@RequestMapping(value="/saveShiftAllotment") 
public {your_return_type} saveShiftAllotment(@RequestParam (value="wdays") int wday)

这篇关于如何通过ajax/jquery在spring控制器中获取复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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