简体   繁体   English

将带有ajax的复选框数据发布到java servlet

[英]Post checkbox data with ajax to java servlet

I have a script that i try to pass a check box value to ajax, somehow not able to.我有一个脚本,我尝试将复选框值传递给 ajax,但不知何故无法传递。 To user, names will displayed, while i use the following to get the IDlist for user selected names.对于用户,名称将显示,而我使用以下内容来获取用户所选名称的 IDlist。

<script>
    var finalidlist = '';
        var checkboxes = $('.selectone');
        for (i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked === true) {
                if (finalidlist.length > 0) {
                    finalidlist += ',';
                }
                finalidlist += checkboxes[i].name;
            }
        }

    $('#selectall').click(function () {
        var checkboxes = $('.selectone');
        for (i = 0; i < checkboxes.length; i++) {
            checkboxes[i].checked = this.checked;
        }
    });
    $('.selectone').click(function () {
    });
</script>

After that, i try to pass the values to ajax when user submit之后,我尝试在用户提交时将值传递给 ajax

<script>
 $(function () {
    $('#submit_button').click(function () { $.ajax({
                url: 'submit.jsp',
                type: 'post',
                dataType: 'html',
                async: false,
                data: {ids: finalidlist}
    }
   };
}

</script>

While i am not able to get the checked id list inside the ajax.虽然我无法在 ajax 中获取已检查的 id 列表。 I am pretty new to jsp, can anyone help on it?我对jsp很陌生,有人可以帮忙吗?

I moved your script for initializing the finalidlist in the click handler.我移动了你的脚本来初始化点击处理程序中的 finalidlist。 This way you don't need to update the list every time a checkbox changed.这样您就无需在每次更改复选框时更新列表。

$(function () {
    $('#submit_button').click(function () {
        var finalidlist = '';
        for (i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked === true) {
                if (finalidlist.length > 0) {
                    finalidlist += ',';
                }
                finalidlist += checkboxes[i].name;
            }
        }
        $.ajax({
            url: 'submit.jsp',
            type: 'post',
            dataType: 'html',
            async: false,
            data: {ids: finalidlist}
        }
    };
});

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

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