简体   繁体   English

在JAVASCRIPT中获取php选中的复选框值

[英]Get php selected checkbox values in JAVASCRIPT

Im new to Javascript and i cannot solve this . 我是Java语言的新手,我无法解决这个问题。 I have a checkbox form , so i need to get the selected value(s) and post to a php file . 我有一个复选框表格,所以我需要获取选择的值并发布到php文件。

Here is the script : 这是脚本:

<script>
                $("#addusers").click(function(){

                    var checkedValue = null;
                    var inputElements = document.getElementsByClassName('values');
                    for(var i=0; inputElements[i]; ++i){
                        if(inputElements[i].checked){
                            checkedValue = inputElements[i].value;
                            break;
                        }
                    }

                    $.post("users_add.php", {
                        checkedValue:checkedValue

                    }, function(data) {
                        alert(data);
                    });
                });
            </script>

Here is the php code : 这是php代码:

<td>
                                <div>
                                    <?php
                                    foreach($userProfiles as $u)
                                    {
                                        foreach($u as $x)
                                        {
                                            echo "<input type=\"checkbox\" name=\"values\"> $x";
                                            echo "<br>";
                                        }
                                    }
                                    ?>

                                </div>
                            </td>

Im checking print_r($_POST) post variables , but it isnt showing anything. 我正在检查print_r($ _ POST)发布变量,但没有显示任何内容。 Is my javascript okay ? 我的JavaScript可以吗?

Don't worry about being new to javascript, we all start sometime. 不用担心成为javascript新手,我们都从某个时候开始。 Regarding your code, first of all your "for cycle" is saving the first value then skipping, i know you are not receiving anything yet but i'm guessing you will get only one value. 关于您的代码,首先,您的“ for cycle”是保存第一个值,然后跳过,我知道您还没有收到任何东西,但是我猜您只会得到一个值。 The most reasonable cause for not getting anything posted is that you are not using the right selector. 没有发布任何东西的最合理原因是您没有使用正确的选择器。 For instance: 例如:
var inputElements = document.getElementsByClassName('values');
will retrieve all html elements that look like this: 将检索所有如下所示的html元素:
<input type="text" class="values" />
Oh sorry i just noticed you did post your html code, since you are already using jQuery there is no need to mix it with vanilla javascript, here is the corrections i found for your code: 哦,对不起,我刚刚注意到您确实发布了html代码,因为您已经在使用jQuery,所以无需将其与普通javascript混合使用,以下是我为您的代码找到的更正:

            var checkedValues = [];
            var inputElements = $('input[type="checkbox"]');
            inputElements.each(function () {
                if($(this).is(':checked')){
                    checkedValues.push($(this).val());
                }
            });                

Explanation: 说明:
var checkedValues = [];
This way we declare an Array in javascript so we can access the array method "push" to insert elements as they meet our condition (checkbox that are checked). 这样,我们在javascript中声明了一个Array,因此我们可以访问数组方法“ push”,以在满足条件的情况下插入元素(选中的复选框)。

var inputElements = $('input[type="checkbox"]'); This is a jQuery selection of the elements of the DOM, we are saying "get all the input elements which have an attribute of type=checkbox. You can learn more about jquery selectors in their oficial site: https://api.jquery.com/category/selectors/ 这是对DOM元素的jQuery选择,我们说“获取具有type = checkbox属性的所有输入元素。您可以在其官方站点https://api.jquery上了解有关jquery选择器的更多信息。 com / category / selectors /

inputElements.each(function () {
    if($(this).is(':checked')){
        checkedValues.push($(this).val());
    }
});

This bit does a few things 这有点做
inputElements.each(function(){});
This will execute a function "for each" element in the inputElements selection. 这将在inputElements选择中为每个元素执行一个函数。 You can get more info about jQuery.each in the official doc: https://api.jquery.com/each/ 您可以在官方文档中获取有关jQuery.each的更多信息: https : //api.jquery.com/each/

if($(this).is(':checked'))
This if asks if the current element in the loop has the "checked" attribute set. 这是否询问循环中的当前元素是否设置了“已检查”属性。 We use $(this) to convert the pure DOM element we got from the original selection into a jQuery object to use the "is" method. 我们使用$(this)将从原始选择中获得的纯DOM元素转换为jQuery对象,以使用“ is”方法。

checkedValues.push($(this).val());
Here we add the VALUE of an element that has the checked attribute set, not the element, not the boolean, but whatever is set in the "value" property of the input. 在这里,我们添加已设置检查属性的元素的VALUE,而不是元素,不是布尔值,而是在输入的“ value”属性中设置的任何值。

Anything that it isn't clear or it isn't working feel free to ask, and happy coding! 任何不清楚或无法正常工作的问题都可以随时询问,并祝您编程愉快! :) :)

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

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