javascript 将选定的选项值从 select 传递给 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17465455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:26:26  来源:igfitidea点击:

pass selected option value from select to php

phpjavascripthtml

提问by Sanjay Malhotra

    <style type="text/css">
    form select {
        display:none;
    }
    form select.active {
        display:block;
    }
    </style>
    <script type="text/javascript">
    window.onload = function () {
        var allElem = document.getElementsByName("sl");
        for (i = 0; i < allElem.length; i++) {
            allElem[i].addEventListener('change', toggleDisplay);
        }
   }
    function toggleDisplay(e) {
       var id = 's' + this.value;
        var currentSelect = document.getElementsByClassName("active")[0];
        if (currentSelect) {
            currentSelect.className = "";
        }
        document.getElementById(id).className = "active";
    }    
    </script>
    </head><body>
    <form action="check_a.php">
    <h2><b>Source: </b></h2>
    <input type="radio" name="sl"  value="c">Central</input>
    <input type="radio" name="sl"  value="h">Eastern</input>
    <input type="radio" name="sl" value="w">Western</input>

    <select id="sc" name="sc">
    <option value="1">1</option>
    <option value="2">2</option>
    </select>

    <select id="sh">
    <option value="3">3</option>
    <option value="4">4</option>
    </select>

    <select id="sw">
    <option value="5">5</option>
    <option value="6">6</option>
    </select>
    <input type="submit" name="submit" value="submit"/>

    </form></body></html>

there is one radio button whose value displays the specific select box, which is done using javascript. the value of tag needs to be passed to php code. How can i pass the selected option value to php?

有一个单选按钮,其值显示特定的选择框,这是使用 javascript 完成的。tag 的值需要传递给 php 代码。如何将选定的选项值传递给 php?

回答by Ben

Can't you just set all the selectelements to have the same name?

您不能将所有select元素设置为具有相同的名称吗?

<select id="sc" name="tagvalue">
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<select id="sh" name='tagvalue'>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<select id="sw" name='tagvalue'>
    <option value="5">5</option>
    <option value="6">6</option>
</select>

Then, just take the value of $_POST['tagvalue'].

然后,只需取 的值$_POST['tagvalue']