如何检查复选框是否已选中 [英] How to check if a checkbox is checked

查看:83
本文介绍了如何检查复选框是否已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上具有复选框

<input class="addToFavorite" type="checkbox" name="addToFavorite"> Add to favorite

现在在表单发布时检查此复选框是否已使用此代码检查。但是它每次都会返回true。

now when form posting i check if this checkbox checked with this code. But it return true every time. how i can check if checkbox has been really checked?

boolean wantAddToFavorites = false;
            if (isPayAction) {
                wantAddToFavorites = request.getParameter("addToFavorite").equals("on");
            } 

FireBug结果

如您所见,它总是发送其值

as you see It always send its value

推荐答案

如果要在服务器端检查是否已选中复选框,则应执行以下操作:

If you want to check on server-side if a checkbox has been checked or not, you should do the following:

1。。将值添加到复选框

<input class="addToFavorite" type="checkbox" name="addToFavorite" value="addToFavourite"> Add to favorite</input> 

2。。在服务器端选中此复选框值

2. Check this checkbox value on server-side

if(request.getParameter("addToFavorite") == null){
    //checkbox not checked
}else{
    //checkbox checked
}

这篇关于如何检查复选框是否已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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