简体   繁体   English

使用Spring Form时,如何获取ajax / jquery中的复选框值

[英]how do i get the checkbox value in ajax/jquery when using spring form

I have a group of checkboxes deefined by path deliveryStatus as shown below 我有一组由路径deliveryStatus定义的复选框,如下所示

<form:checkbox path="deliveryStatus" value="notDelivered"/>
<form:checkbox path="deliveryStatus" value="delivered"/>

I read two examples which are very nice 我读了两个很好的例子

Example #1 例子1

Example #2 from StackOverflow Example#2来自StackOverflow

I notice the ID comes as deliveryStatus1 and deliveryStatus2 and not deliveryStatus therefore approach mentioned in Example #1 does not work for me directly. 我注意到该ID的名称是deliveryStatus1和deliveryStatus2,而不是deliveryStatus,因此示例1中提到的方法对我不直接起作用。 Also when using the solution mentioned in example #2 (considering the parent tag ID to be deliveryStatus) I do not get any result. 同样,在使用示例2中提到的解决方案时(考虑将父标记ID设置为deliveryStatus),我也没有得到任何结果。

$('#deliveryStatus').is(":checked");

returns false and 返回false和

$('#deliveryStatus').val();

gives me undefined 给我不确定的

What is the correct way to get the selected/checked checkbox for my form(deliveryStatus) in jquery/ajax. 什么是在jquery / ajax中获取我的表单(deliveryStatus)的选中/选中复选框的正确方法。

I noticed below code works after I realized IDs to be deliveryStatus1 and deliveryStatus2 在意识到ID为deliveryStatus1和deliveryStatus2之后,我注意到以下代码有效

var deliverystatus =  $('#deliveryStatus1').val();
alert( 'deliverystatus' );
deliverystatus = $('#deliveryStatus1').is(":checked");
alert( deliverystatus );
deliverystatus = $('#deliveryStatus2').is(":checked");
alert( deliverystatus );

But do not feel this is the correct way to get the checked option. 但是不要以为这是获取选中选项的正确方法。 Reason being there are multiple checkboxes. 原因是有多个复选框。

Reading the Example #1 I am considering there should be a better way to read those multiple checkboxes with same path value 阅读示例1,我认为应该有一种更好的方法来读取具有相同路径值的多个复选框


Update to Question 更新问题

Explicitly adding ID like below 像下面这样显式添加ID

<form:checkbox path="deliveryStatus" id="deliveryStatus" value="notDelivered"/>
<form:checkbox path="deliveryStatus" id="deliveryStatus" value="delivered"/>

allows me to get value by 让我通过获得价值

$('#deliveryStatus:checked').val();

This is valid only if there is one selection. 仅当选择一个时有效。 Therefore may not be very correct when there is multiple selection 因此,当有多个选择时可能不是很正确

When you have multiple check boxes with same path variables, they will be rendered with same names like below. 当您有多个具有相同路径变量的复选框时,它们将以相同的名称呈现,如下所示。

<form:checkbox path="deliveryStatus" value="notDelivered"/>
<form:checkbox path="deliveryStatus" value="delivered"/>

Will be rendered as 将呈现为

<input id="deliveryStatus1" name="deliveryStatus" type="checkbox" value="notDelivered"/>
<input id="deliveryStatus2" name="deliveryStatus" type="checkbox" value="delivered"/>

You can obtain the values of checkbox that were checked using the below. 您可以使用以下方法获取选中的复选框的值。

$("input[name='deliveryStatus']:checked").each(function (){
    alert($(this).val());
});

If you just want to obtain the values of all checked no matter if they are checked or not, use the below. 如果您只想获取所有已检查值,而不管是否选中它们,请使用以下内容。

$("input[name='deliveryStatus']").each(function (){
    alert($(this).val());
});

Hope this helps. 希望这可以帮助。

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

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