选择复选框


Selecting checkboxes

我正在处理的这个项目有一些问题。

我有一个不同的复选框列表,这些复选框是使用PHP从数据库中提取的记录。

现在,当我点击其中一个复选框时,我希望该选项以某种方式显示在其他地方。我一直在尝试让它与jQuery一起使用,但我没有那么多经验,所以我真的没什么可做的。

这实际上是通过使用AJAX完成的。但是,如果您了解/想了解jQuery,您应该了解jQuery中的AJAX。在jQuery中,您必须调用一个和复选框中特定id的点击事件相关联的函数。对于Simplicity,让我们考虑每个唯一的Id都被赋予每个og复选框,然后您可以单独调用jQueryClick事件来处理它。

<script>
$(function(){
$("#yourid").click(function() {
//do what you want to do here if checkbox "yourid" is clicked
});
$("#yournextid").click(function(){
//do what your next event is.
});
});
</script>

这里的"yourid"、"yournextid"是各自复选框的id

保留一个隐藏变量来存储选中复选框的值。

正如"cipher"所建议的,在javascript函数中提到复选框的onclick事件。将复选框对象作为参数传递。

在功能中,检查复选框是否选中。如果选中,则将复选框的值添加到隐藏变量中。如果未选中,则从隐藏变量中删除该值,以防之前已选中该值。

通过这种方式,您总是有选中复选框的列表。

我解决了我遇到的问题。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
    $('.checkbox').change(function(event){
        checked_value = $(this).val();          
        if(this.checked)
        {
                            $("#content").append("<div id='" + checked_value +"'>New value: " + checked_value + "</div>");
        }
        if( !this.checked )
        {
            $("#" + checked_value).remove(); 
        }
    });
            $('#checkall').change(function(){
                    $('.checkbox').attr('checked',$(this).attr('checked'))
            });
});
</script>   
    </head>
    <body>
    <input type="checkbox" id="cb1" class="checkbox" value='a' />
    <input type="checkbox" id="cb2" class="checkbox" value='b' />
    <input type="checkbox" id="cb3" class="checkbox" value='c' />
<div id="content"></div>
</body>
</html>

上面的代码就是我现在使用的代码。

我不记得我是从哪里得到的,但要归功于它各自的所有者。