简体   繁体   English

如何通过单选按钮将相同的类/ ID名称php值传递给javascript?

[英]How to pass same class/id name php values from radio button to javascript?

This is my problem. 这是我的问题。 I am looping items from the database that contains items name and id. 我正在从包含项目名称和ID的数据库中循环项目。 I have a radio button for each items. 每个项目都有一个单选按钮。 The id of that item is stored in the radio buttons value ....Now what i want to achieve is when i click on the submit button,it will call a function call save() in javascript and save all the selected items ONLY .... i've been trying this for hours but i cant get it worked. 该项目的ID存储在单选按钮值中。现在,我要实现的是单击提交按钮时,它将在javascript中调用函数调用save()并仅保存所有选定的项目。 ...我已经尝试了几个小时,但我无法使其正常工作。

Here are my codes.... 这是我的代码...

When i do my while looping from the DB, it will display the radio button and the items name... 当我从数据库循环时,它将显示单选按钮和项目名称...

<?php
    $retrieveQuery = "SELECT * FROM `itemdb` WHERE `approve`= 1 AND `userId` = '$userId';";
    $retrieveresult = mysqli_query($dbconnect, $retrieveQuery);
    if(mysqli_num_rows($retrieveresult) > 0)
    {                                                   
        while($retrieverow = mysqli_fetch_assoc($retrieveresult))
        {
?>
            <p>&nbsp;</p>                                                       
            <div style="background-color: rgba(0, 0, 0, 0.16);">                                                                
                <input style="margin-bottom: -19px;" type="checkbox" class="item" name="item[]" value=<?php echo $retrieverow['itemID'] ?>>
                <p style="margin-left: 26px;"><?php echo $retrieverow['itemName'] ?></p>
                <p style="margin-top:-10px;margin-left:28px"><i><?php echo $retrieverow['category'] ?></i></p>
            </div>                                                                                                          
<?php
        }                                                   
    }
    else
    {
        echo "Test : There is no approve Items to be displayed";
        echo "Either wait for the admin to approve or upload items to start Trading ! ";
    }
?>

This is my script:(How can i save my items?) 这是我的脚本:(如何保存我的物品?)

function save()
{
    var f = document.getElementsByClassName('item').value; //my own items chosen frm invntry
    xmlhttp.onreadystatechange = function(){
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                            document.getElementById("confirmTrade").innerHTML = xmlhttp.responseText;
                        }
                        };
                        xmlhttp.open("POST","trade.php?f="+f,true);
                        xmlhttp.send();
}

Right after i click the save() function it will send the data to another page using ajax method.... 在我单击save()函数后,它将使用ajax方法将数据发送到另一页。

PS: Everything works fine...Other variables are passed successfully but only one item variable is being passed .... PS:一切正常...其他变量已成功传递, 但仅一项变量已传递 ...。

Thanks in advance! 提前致谢!

Without looking into it too much, take a look at this line: 无需过多研究,请看以下这一行:

var f = document.getElementsByClassName('item').value;

document.getElementsByClassName gets an HTMLCollection of elements, so calling value on this doesn't make much sense. document.getElementsByClassName获取元素的HTMLCollection ,因此对此调用值没有多大意义。 You'll need to loop through them and find the selected ones. 您需要遍历它们并找到所选的。 I have it building an array then joining it into a comma separated list: 我让它建立一个数组,然后将其加入逗号分隔的列表中:

var checkboxes = document.getElementsByClassName('item');
var f = [];

for(var i = 0; i < checkboxes.length; i++){
   if(checkboxes[i].checked == true)
       f.push(checkboxes[i].value);
}

f = f.join();//makes comma seperated list

//proceed with ajax call

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

相关问题 Javascript,切换具有相同名称的单选按钮图像 - Javascript, toggle radio button images with the same name Ruby 如何将值从单选按钮传递到 Controller 方法 - Ruby How To Pass Values From Radio Button To Controller Method 如何通过使用 PHP 和 Javascript 获取单选按钮值来计算总数? - How to calculate a total by taking the radio button values using PHP and Javascript? 如何通过单选按钮将所选值传递给javascript函数 - How to pass selected value to javascript function from radio button 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 如何清除所有具有相同名称的单选按钮。 我想将名称作为变量传递 - How to clear all radio button with same name . I want to pass name as variable Javascript:当使用同一类检查其他 15 个单选按钮时,如何启用单选按钮? - Javascript: How can I enable a radio button when 15 other radio buttons are checked with the same class? 如何将值列表传递到单选按钮选择中 - How to pass list of values into a radio button selection 从取消选择的单选按钮中删除班级名称 - Remove class name from deselected radio button 从内联的javascript启动单选按钮名称 - Initiate the radio button name from javascript in inline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM