简体   繁体   English

HTML / PHP:基于一个单选选项传递多个值

[英]HTML/PHP: Pass multiple values based on one radio option

I have a simple radio selection and I'm trying to pass two values to the next page. 我有一个简单的单选,并且试图将两个值传递给下一页。 Product name as well as price. 产品名称以及价格。 How can assign both bits of info to the same radio choice? 如何将信息的两位分配给同一无线电选择?

My inclination is to use hidden fields, but I'm not sure how to link them to their respective radio choice. 我倾向于使用隐藏字段,但不确定如何将它们链接到各自的广播选择。

Hidden fields won't work, but this does: 隐藏的字段将不起作用,但这可以:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Demo Send multiple radio data</title>
    <script>
        function getRadioData(radios) {
            window.radioValue = null;
            window.radioId = null;
            window.radioName = null;
            window.radioClass = null;
            for (var i=0; i<radios.length; i++) {
                var radio = radios[i];
                if (radio.checked) {
                    radioValue = radio.value;
                    radioId = radio.id;
                    radioName = radio.name;
                    // radioClass = radio.className; // no class given yet
                    break;
                }
            }
            return;
        }

        function sendRadioData(groupName) {
            var theGroup = theForm.elements[groupName];
            getRadioData(theGroup);
            // console.log(radioValue,radioId,radioName); 
            if (radioValue == null) {
                alert('No radio checked');
                return false;
            }
            else {
                window.location.href = 'nextpage.php?radioValue='+radioValue+'&radioId='+radioId+'&radioName='+radioName;
            }
        }
    </script>
</head>
<body>
    <form name="theForm" id="theForm" action="nextpage.php">
        <input type="radio" name="yourName" value="10" id="firstId"><br>
        <input type="radio" name="yourName" value="20" id="secondId"><br>
        <input type="radio" name="yourName" value="30" id="thirdId"><br>
        <input type="radio" name="yourName" value="40" id="fourthId"><br>
        <input type="button" value="Send radio data" onclick="sendRadioData('yourName')">
    </form>
</body>
</html>

.

As you can see, you can even send four easily retrievable data bits per clicked radio button. 如您所见,您甚至可以为每个单击的单选按钮发送四个易于检索的数据位。 Only the name stays the same. 只有名称保持不变。 Just make sure to adapt your nextpage.php to the GET variables. 只要确保使nextpage.php适应GET变量即可。

No live demo with this, because you can only see the result in the console or the address bar. 对此没有实时演示,因为您只能在控制台或地址栏中看到结果。

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

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