从阿贾克斯选中的复选框中获得价值 [英] getting value from checked checkbox in ajax

查看:86
本文介绍了从阿贾克斯选中的复选框中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里选中的复选框的值,但它是不确定的,因为它成为一个数组。请帮我该怎么办呢..谢谢..

 <形式GT;
<输入类型=复选框值=名称=函[]/>
<输入类型=复选框值=BNAME =函[]/>
<输入类型=复选框值=CNAME =函[]/>
<输入类型=按钮值=提交的onclick =拉尼()/>
< /形式GT;
<脚本>
功能拉尼(){
    $阿贾克斯({
        类型:后,
        网址:ajax.php
        数据:{
            妲:$([名称=号])VAL()。
            },
        成功:函数(MSG){
            警报(味精);
            }
        })
    }
< / SCRIPT>
 

解决方案

在这里,你会得到一个变量选中的复选框值 checkbox_value 后来被 |

  $(#BTN _)。在('点击',函数(){
    VAR checkbox_value =;
    $(:复选框)每个(函数(){。
        VAR器isChecked = $(本)。是(:选中);
        如果(器isChecked){
            checkbox_value + = $(本).VAL()+|;
        }
    });
    警报(checkbox_value);
    //你真棒code调用AJAX
});
 

样品展示

学习jQuery:简单和容易的jQuery教程博客

I want to get the values of the checked checkbox here but it is undefined because it becomes an array.. Please help me how to do it.. thank you..

<form>
<input type="checkbox" value="a" name="letter[]" />
<input type="checkbox" value="b" name="letter[]" />
<input type="checkbox" value="c" name="letter[]" />
<input type="button" value="submit"  onclick="rani()" />
</form>
<script>
function rani(){
    $.ajax({
        type:"post",
        url:"ajax.php",
        data:{
            radha:$("[name=letter]").val(),
            },
        success:function(msg){
            alert(msg);
            }
        })
    }
</script>

解决方案

Here you will get selected checkbox values in a variable checkbox_value and later split by |.

$("#btn_").on('click', function () {
    var checkbox_value = "";
    $(":checkbox").each(function () {
        var ischecked = $(this).is(":checked");
        if (ischecked) {
            checkbox_value += $(this).val() + "|";
        }
    });
    alert(checkbox_value);
    // your awesome code calling ajax
}); 

Sample Demo

Learn jQuery: Simple and easy jQuery tutorials blog

这篇关于从阿贾克斯选中的复选框中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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