无法检索复选框状态 [英] Cannot retrieve the checkbox status

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

问题描述

我有一个表格,每一行都有一些复选框。表脚中有一个select元素,有几个选项。当用户选择一个选项时,我想将任何选中的复选框的值和所选的选项传递给php脚本。

I have a table with some checkboxes in every row. There is a select element in the table footer with several options. When the user chooses an option, i want to pass the value of any checked checkboxes and the selected option to a php script.

这是一种形式:

<form name="doall" method="POST" action="action.php">
    <table>
        <tbody>
            <?php for($j=0;$j<count($userToRead);$j++){ ?>
                <tr>
                    <td>
                        <input type="checkbox" id="selection_<?php echo $j ?>" name="objectSelected[<?php echo $userToRead[$j]['id'] ?>]" value="1"/>
                    </td>
                    <td align="center">
                        <?php echo $userToRead[$j]['id'] ?>
                    </td>
                    <td align="center" style="text-align:center;padding-left:15px;background-color:<?php echo $SListsStatus[intval($userToRead[$j]['userstatus'])][1]; ?>;" >
                    </td>
                    <td align="center">
                        <?php echo stripaccenti($userToRead[$j]['user']) ?>
                    </td>
                    <td>
                        <?php echo stripaccenti($SUserType[$userToRead[$j]['type']]['name']) ?>
                    </td>
                    <td align="center">
                        <?php echo stripaccenti($userToRead[$j]['first_name']) ?>
                    </td>
                    <td align="center">
                        <?php echo stripaccenti($userToRead[$j]['last_name']) ?>
                    </td>
                    <td align="center">
                        <?php echo stripaccenti($userToRead[$j]['title']) ?>
                    </td>
                    <td class="actBtns">
                        <a href="#" title="Update" class="tipS">
                            <img src="images/icons/edit.png" alt="" />
                        </a>
                        <a href="#" title="Remove" class="tipS">
                            <img src="images/icons/remove.png" alt="" />
                        </a>
                    </td>
                </tr>
            <?php } // end for ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="9">
                    <div class="itemActions">
                        <label>Con tutte le selezionate:</label>
                        <select name="whatDoAll">
                            <option value="1">Attiva</option>
                            <option value="2">Disattiva</option>
                        </select>
                        <input type="submit" value="Esegui" class="button redB" style="margin: 5px;"/>
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>                
</form>

所以在我的表的每一行我有一个复选框,值是db对象的ID我需要用我的php脚本修改。

So in every line of my table I have a checkbox where the value is the ID of the db object that I need to modify with my php script.

这是我的脚本:

$('input[type="submit"]').click(function() {
    var output = '';
    $('input[type="objectSelected"]:checked').each(function(index) {
        output += $(this).val() + ", ";
    });
    alert(output + "is checked!");
});

在php文件中,我尝试检索变量 $ _ POST [' objectSelected'] ,但变量始终为空。

In the php file i try to retrieve the variable with $_POST['objectSelected'] but the variable is always empty. Also, the script alert is empty.

这些表是使用jquery创建的:如果我使用纯html表与纯html复选框一切正常!

The tables are create with jquery: if I use a pure html table with pure html checkboxes everything works!

推荐答案

你做错了。您正在指定输入类型,代码应为:

You are doing wrong. You are specifying input type then the code should be:

<script type="text/javascript">
                    $('input[type="submit"]').click(function() {
                      var output = '';

                      $('input[type="checkbox"]:checked').each(function(index) {
                         output += $(this).val() + ", ";
                      });

                      alert(output + "is checked!");
                    });
  </script>

希望这可以帮助你

这篇关于无法检索复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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